【问题标题】:Quickblox - Flutter JNI was detached from native C++Quickblox - Flutter JNI 与本机 C++ 分离
【发布时间】:2021-01-07 20:11:51
【问题描述】:

我在 Flutter 应用程序中使用 Quickblox 进行聊天。建立连接后,聊天工作正常。当应用程序发送到后台时,我按照 Quickblox 文档的建议将连接设置为关闭。但是当我重新打开我的应用程序时,它在其事件(QBChatEvents.RECEIVED_NEW_MESSAGE) 中不再收到消息。尽管在日志中发送和接收消息,但此事件不再起作用。并且日志显示了这个异常,

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

这是我订阅的活动,

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

我已经从他们的文档中添加了这个实现。

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;
}

我在这里做错了什么?

【问题讨论】:

  • 你有没有设法解决这个问题?我有一个类似的问题。也收到 JNI 分离消息,但使用不同的模块。

标签: android flutter chat quickblox


【解决方案1】:

尝试向 Flutter 发送平台消息,但 FlutterJNI 与原生 C++ 分离。无法发送。渠道: 方法通道被销毁时会出现此错误
当我们杀死应用程序并尝试从本机代码传达颤振时,就会发生此运行时错误

解决方案如下: 在后台创建一个新的方法通道,并用这个通道调用颤振部分。 如下所示

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");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 2023-02-13
    • 2021-01-03
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多