首页 > 解决方案 > Flutter FCM on resume on launch and on message not working in ios 缺少插件实现在启动时触发

问题描述

Flutter FCM on resume on launch and on message not working in ios缺少插件实现dart服务在启动时触发,在android上工作正常以下是用于处理fcm的代码。在IOS中,当应用程序在后台和点击时触发通知应用程序已打开


 _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print(message);
        FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
            new FlutterLocalNotificationsPlugin();
        // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
        var initializationSettingsAndroid =
            new AndroidInitializationSettings('launcher_icon');
        var initializationSettingsIOS = new IOSInitializationSettings(
            onDidReceiveLocalNotification: onDidReceiveLocalNotification);
        var initializationSettings = new InitializationSettings(
            initializationSettingsAndroid, initializationSettingsIOS);
        flutterLocalNotificationsPlugin.initialize(initializationSettings,
            onSelectNotification: onSelectNotification);
        var androidPlatformChannelSpecifics = AndroidNotificationDetails(
            '123', 'convoy notification', 'convoy notification',
            importance: Importance.Max,
            priority: Priority.High,
            ticker: 'ticker');
        var iOSPlatformChannelSpecifics = IOSNotificationDetails();
        var platformChannelSpecifics = NotificationDetails(
            androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
        await flutterLocalNotificationsPlugin.show(
            0,
            message['notification']['title'],
            message['notification']['body'],
            platformChannelSpecifics,
            payload: message['data']['payload']);
      },
      onBackgroundMessage: myBackgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        var string = message["data"]["payload"];
        var payload = jsonDecode(string);
        print(payload);
        if (payload != null) {
          var type = payload["refresh"];
          if (type == "OFFERS") {
            Future.delayed(Duration(seconds: 3), () {
              _navigateToOffers(payload["id"]);
            });
          } else if (type == "REQUESTS") {
            Future.delayed(Duration(seconds: 3), () {
              _navigateToRequests(payload["id"]);
            });
          }
        }
        //_navigateToItemDetail(message);
      },
      onResume: (Map<String, dynamic> message) async {
        var string = message["data"]["payload"];
        var payload = jsonDecode(string);
        print(payload);
        if (payload != null) {
          var type = payload["refresh"];
          if (type == "OFFERS") {
            _navigateToOffers(payload["id"]);
          } else if (type == "REQUESTS") {
            _navigateToRequests(payload["id"]);
          }
        }
        // _navigateToItemDetail(message);
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
  }

标签: iosflutterfirebase-cloud-messaging

解决方案


尝试在 ios/Runner/AppDelegate.swift 中删除这些行:

if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate }

我遇到了同样的问题,删除此代码后,我的应用程序运行良好。


推荐阅读