【问题标题】:Flutter - Firebase Cloud Messaging, Data Message is not received on iOSFlutter - Firebase 云消息传递,iOS 上未收到数据消息
【发布时间】:2020-10-19 04:55:19
【问题描述】:

通知处理程序

void firebaseCloudMessagingListeners() {
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('onMessage ==> $message');
        messageHandler(message);
      },
      onResume: (Map<String, dynamic> message) async {
        print('onResume ==> $message');
        messageHandler(message);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print('onLaunch ==> $message');
        messageHandler(message);
      },
      onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
    );
  }

  static Future<dynamic> myBackgroundMessageHandler(
      Map<String, dynamic> message) {
    print('onBackgroundMessage ==> $message');
    if (message.containsKey('data')) {
      final dynamic data = message['data'];
      print('$data');
    }

    if (message.containsKey('notification')) {
      final dynamic notification = message['notification'];
      print('$notification');
    }

    return null;
  }

发布https://fcm.googleapis.com/fcm/send

{
   "to": "myFCMToken",
   "priority": "high",
   "data": {
      "click_action": "FLUTTER_NOTIFICATION_CLICK",
      "data_title": "data_title",
      "data_body": "data_body"
   },
   "notification": {
      "title": "Good Night",
      "body": "Wish you have a nice dream..",
      "sound": "default"
   }
}

当我发送带有上述有效负载的通知时,通知被传递到系统托盘,当我点击它时,我遇到了以下三种情况:

  1. 应用处于后台(关闭/终止),显示“数据”并调用 onLaunch 方法。
  2. 应用处于后台(最小化),“数据”丢失,未调用 onResume 方法。
  3. 应用在前台,“数据”丢失,onMessage 方法没有被调用。

问题是,如何接收或处理带有通知的“数据”也传递到系统托盘? 任何帮助将不胜感激。

【问题讨论】:

    标签: ios flutter dart push-notification firebase-cloud-messaging


    【解决方案1】:

    IOS 消息具有不同的结构,您不需要 IOS 的“数据”,例如可以是

         if(Platform.isAndroid){
              roomId = message['data']['chatRoomId'];
              senderId = message['data']['senderId'];
            }else if(Platform.isIOS){//without data
              roomId = message['chatRoomId'];
              senderId = message['senderId'];
            }
    
    

    【讨论】:

      【解决方案2】:

      在 iOS 上,数据直接附加到消息中,并且省略了附加的数据字段。 要在两个平台上接收数据,您的 myBackgroundMessageHandler() 函数应如下所示:

      Future<void> myBackgroundMessageHandler(Map<dynamic, dynamic> message) async {
          var data = message['data'] ?? message;
          print(data);
          String expectedAttribute = data['expectedAttribute'];
        }
      

      您可以在 Firebase Messaging Flutter 文档的“带有附加数据的通知消息”部分下了解更多信息。

      【讨论】:

      • 以一种聪明、原创和创造性的方式
      • 我不知道为什么,但是 if(Platform.isAndroid){message = message["data"];} 不起作用。
      • 请在此处粘贴您在 message["data"] 上收到的内容
      猜你喜欢
      • 2018-11-21
      • 2020-06-16
      • 2020-07-16
      • 2019-02-06
      • 2021-09-27
      • 2019-07-12
      • 2019-10-08
      • 2017-05-05
      • 2020-07-28
      相关资源
      最近更新 更多