channel.invokeMethod() 在 FirebaseMessaging.onBackgroundMessage() 中不起作用;

问题描述 投票:0回答:1

在颤振中 在 a 中调用像这样的原生方法

_channel.invokeMethod()

FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);


例外

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method wakeupscreen on channel flutter_native)


这是我的 _firebaseMessagingBackgroundHandler()

@pragma('vm:entry-point') Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { print("Handling a background message: ${message.messageId}"); await Firebase .initializeApp(); //make sure firebase is initialized before using it (showCallkitIncoming) wakeupscreen(); } // wakeupscreen() func -- this is a global function so does not require any class instance to run wakeupscreen() async { try { const platform = MethodChannel('flutter_native'); await platform.invokeMethod('wakeupscreen'); } on PlatformException catch (e) { print("Failed to invoke the method: '${e.message}'."); } }
我尝试在普通代码中使用相同的wakeupscreen()函数:

InkWell( onTap: () { Future.delayed(Duration(seconds: 5), () { wakeupscreen(); }); }, child: Text("wakeup in 5 sec"), ),
正常调用时,它可以完美地工作,但是一旦在 

_firebaseMessagingBackgroundHandler

 中调用相同的函数,它就会抛出异常。

android flutter dart firebase-cloud-messaging flutter-native
1个回答
0
投票
在main.dart中,设置一个方法通道来处理来自后台isolate的消息:

// Inside main.dart MethodChannel _channel = MethodChannel('flutter_native'); // Set up method channel to communicate with background isolate _channel.setMethodCallHandler((call) async { if (call.method == 'wakeupscreen') { wakeupscreen(); // Call your wakeupscreen function here } });
从wakeupscreen函数中删除方法通道初始化:

// Remove this line from the wakeupscreen function const platform = MethodChannel('flutter_native');
这些更改确保方法通道被正确配置为管理来自后台隔离的消息,并确保根据需要调用wakeupscreen函数。

© www.soinside.com 2019 - 2024. All rights reserved.