问题描述
在我的Flutter App中,我想从本机iOS代码(从AppDelegate类的通知接收器)调用dart方法,并且我正在使用带有invokeMethod的通道来实现这一点,从dart方面,一切都已正确设置,并且从iOS双方一切都设置正确并且调用正确进行,但是从未调用过dart方法调用处理程序,并且从未收到过iOS中的invokeMethod调用的回调,不知道我是否缺少某些内容,这是我双方的代码:
在Swift端:
var channel = FlutterMethodChannel()
// channel is created when registering app plugins
func registerPlugins(with registry: FlutterPluginRegistry) {
let mainStoryBoard = UIStoryboard(name: "Main",bundle: nil)
let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "AppIdentifier") as! FlutterViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = redViewController
let controller : FlutterViewController = self.window.rootViewController as! FlutterViewController;
self.channel = FlutterMethodChannel(name: "myApp.main/backgroundNotifications",binaryMessenger:
controller.binaryMessenger);
}
// the background notification receiver
override func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
// comes here
DispatchQueue.main.async {
self.channel.invokeMethod("toTo",arguments: userInfo,result: {(r:Any?) -> () in
print(r.debugDescription); // Never comes here
})
}
// comes here with no errors of execution to previous call
}
在Dart侧:(在我的一个小部件类中)
class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
MethodChannel _channel =
const MethodChannel('myApp.main/backgroundNotifications');
@override
void initState() {
super.initState();
.
.
.
_channel.setMethodCallHandler(toTo);
}
Future<dynamic> toTo(MethodCall call) async {
print("init state setMethodCallHandler ${call.method}"); // Never comes here
switch (call.method) {
case 'toTo':
return 145.0;
case 'bar':
return 123.0;
default:
throw MissingPluginException('notImplemented');
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)