Quickblox - Flutter JNI was detached from native C++
Asked Answered
M

1

6

I am using Quickblox for my chats in my Flutter app. Once a connection is made, the chat works fine. When the app is send to the background, I set the connection to closed as advised by Quickblox documentation. But when I reopen my app, it does not receive messages anymore in its event (QBChatEvents.RECEIVED_NEW_MESSAGE). Although the messages are sent and received in the logs but this event does not work anymore. And the logs show this exception,

Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel:

Here's the event that I subscribe to,

QB.chat.subscribeChatEvent(QBChatEvents.RECEIVED_NEW_MESSAGE,
      (data) {
         // my implementaion here
      });

I have added this implementation from their documentation.

class _SomeScreenState extends State<SomeScreen> with WidgetsBindingObserver {
  
@override
initState() {
  super.initState();
  WidgetsBinding.instance.addObserver(this);
}

@override
void dispose() {
  WidgetsBinding.instance.removeObserver(this);
  super.dispose();
}
  
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  switch (state) {
    case AppLifecycleState.resumed:
      try {
        await QB.chat.connect(userId, userPassword);
        } on PlatformException catch (e) {
        // Some error occured, look at the exception message for more details
        }
      break;
    case AppLifecycleState.paused:
      print("app in paused");
      try {
        await QB.chat.disconnect();
      } on PlatformException catch (e) {
        // Some error occured, look at the exception message for more details
        }
      break;
}

What am I doing wrong here?

Medallist answered 21/9, 2020 at 17:29 Comment(1)
Did you ever manage to fix this? I have a similar problem. Also getting the JNI detached message, but with a different module.Unproductive
D
0

Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: This error occurs when method channel is destroyed
when we killed the app and try to communicate the flutter from native code then this run time error occured

Solution is below: create a new method channel in background and call the flutter part with this channel. like as below

fun  createMethodChannel(): MethodChannel? {

    var backgroundMethodChannel: MethodChannel? = null;
    var engine: FlutterEngine? = null;
    if (getEngine() == null) {
        engine = FlutterEngine(mContext!!)
        // Define a DartEntrypoint
        val entrypoint: DartExecutor.DartEntrypoint =
            DartExecutor.DartEntrypoint.createDefault()
        // Execute the DartEntrypoint within the FlutterEngine.
        engine.dartExecutor.executeDartEntrypoint(entrypoint)
    } else {
        engine = getEngine();
    }

    backgroundMethodChannel = MethodChannel(engine?.dartExecutor?.binaryMessenger, "CHANNEL_NAME")
    return backgroundMethodChannel
}
 fun getEngine(): FlutterEngine? { 
    return FlutterEngineCache.getInstance().get("CHANNEL_NAME");
}
Devilkin answered 23/11, 2021 at 14:32 Comment(1)
Hey Sunny. What do you mean by create method channel in background? What file should this snippet be in? And how do we call it in our dart code? Thank you.Gorge

© 2022 - 2024 — McMap. All rights reserved.