0

我已经定义了一个从 AppDelegate.swift 到 Flutter 的 MethodChannel,它触发了 Flutter dart 代码中注册的回调。目标是在 App 收到 FCM Push Notification 时,根据notification_type参数打开特定屏幕。

方法通道成功触发了回调(我可以在控制台中看到颤振回调方法的打印结果),但是如果我尝试从回调方法执行与 UI 相关的任何事情,它永远不会被触发。整个用户界面被冻结,不仅物理点击工作。看起来 MethodChannel 与 ViewController 搞砸了。

这是触发 MethodChannel 的代码

    override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        
        //let rootViewController : FlutterViewController = window?.rootViewController as! FlutterViewController
        
        let rootViewController : FlutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
        let channelName = "mimbr_notification"
        let methodChannel = FlutterMethodChannel(name: channelName, binaryMessenger: rootViewController as! FlutterBinaryMessenger)
        //
        print("CALLING METHOD IN METHOD CHANNEL ....")
        let respStr : String = response.notification.request.content.userInfo["notification_type"] as! String
        methodChannel.invokeMethod("notificationReceived", arguments:respStr)
        
        
      // tell the app that we have finished processing the user’s action / response
      completionHandler()
    }

在 runApp() 之前的 main.dart 中,我注册了回调

final methodChannel = MethodChannel(channelName);
  methodChannel.setMethodCallHandler(_didRecieveNotification);

回调方法是这样的

Future<void> _didRecieveNotification(MethodCall call) async {
    
    final String notification_type = call.arguments;
    print("NOTIFICATION CLICK CALLBACK : $notification_type");
    settingBox.put("notification_clicked", notification_type);
    pcontroller.jumpToTab(0);
  }

当我点击通知时,我在 Xcode 控制台输出上打印了此错误

2022-02-07 13:47:56.229574-0600 Runner[14897:5699730] [VERBOSE-2:FlutterViewController.mm(131)] The supplied FlutterEngine <FlutterEngine: 0x13fa09060> is already used with FlutterViewController instance <FlutterViewController: 0x13f01bc00>. One instance of the FlutterEngine can only be attached to one FlutterViewController at a time. Set FlutterEngine.viewController to nil before attaching it to another FlutterViewController.

我可以看到打印,我可以确认 HiveBox 设置了正确的值,但是某些东西正在杀死 ViewController,是因为调用 MethodChannel 时出错吗?

所有帮助将不胜感激!!

4

0 回答 0