hiennguyen92 / flutter_callkit_incoming

Flutter Callkit Incoming
https://pub.dev/packages/flutter_callkit_incoming
MIT License
180 stars 312 forks source link

lateinit property instance has not been initialized at com.hiennv.flutter_callkit_incoming.FlutterCallkitIncomingPlugin #545

Open redevrx opened 5 months ago

redevrx commented 5 months ago

How to create instance FlutterCallkitIncomingPlugin in native background service

 if(!FlutterCallkitIncomingPlugin.hasInstance()){
                      ///new instance callkit
                      FlutterCallkitIncomingPlugin()
                      FlutterCallkitIncomingPlugin.
                      initSharedInstance(context,engine.dartExecutor.binaryMessenger)
                  }
hbali commented 3 months ago

@redevrx

hi, no idea if your issue still persist but i had the same problem and after hours of trying to solve it, chatgpt helped me 😆

add this function to your class (notification extension, right?)

import java.nio.ByteBuffer

private fun initializeFlutterCallkitIncomingPlugin(context: Context) {
        if (!FlutterCallkitIncomingPlugin.hasInstance()) {
            val binaryMessenger = object : BinaryMessenger {
                override fun send(channel: String, message: ByteBuffer?) {
                    // No-op
                }

                override fun send(channel: String, message: ByteBuffer?, callback: BinaryMessenger.BinaryReply?) {
                    // No-op
                }

                override fun setMessageHandler(channel: String, handler: BinaryMessenger.BinaryMessageHandler?) {
                    // No-op
                }
            }
            FlutterCallkitIncomingPlugin.initSharedInstance(context, binaryMessenger)
        }
    }

and call before you call into the plugin:

initializeFlutterCallkitIncomingPlugin(context)

that solved my problem and all calls are properly coming in even if app is killed, background etc!

hbali commented 3 months ago

@hiennguyen92 you might wanna take a look at this as currently the plugin cannot be called when the app is killed (terminated state).

Obviously my snippet is a workaround but if you have any idea how to implement this properly that would be great!

redevrx commented 3 months ago

Hello, I have resolved this issue by using a Flutter loader. When the Flutter initialization is successful, I will call incoming.

hbali commented 3 months ago

@redevrx ah so you are not calling from notification extension? When the app is terminated there is no flutter instance anywhere so I cannot init flutter yet. it's being initialized after the user accepts the call and the app opens.

redevrx commented 3 months ago

I receive notifications from native and open incoming notifications. As for starting the Flutter engine, we can cache the engine by checking if there is a cache. If not, we create a new engine. If you don't do this, the accept call button won't work if pressed when the app is closed.

hbali commented 3 months ago

then maybe our use case is different. for me it's a video call so the app starts automatically when the user accepts the call so it's not a normal "call only" UI, that might be the difference here.