我应该如何在 flutter 中访问通知负载的数据元素?

How Should I access the data elements of the notification payload in flutter?

以下是 flutter 应用程序中 FCM 通知的云函数中的通知有效负载,当在 flutter 应用程序中收到通知时,我无法弄清楚如何访问 data:{} 的元素

有效载荷

const payload = {
                    notification: {
                      title: `NOTIFICATION2 `,
                      body: contentMessage,
                      badge: '1',
                      sound: 'default'
                    },
                    data: {
                             click_action: 'FLUTTER_NOTIFICATION_CLICK',
                             notification2: notificationid2,
                             detail: detail,
                             senderAvatarURL: messageRecieverSenderAvatar,
                             category: 'default'
                           }
                  }

通知代码

firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {

      print('onMessage: $message');
      Platform.isAndroid ? showNotification(message['notification']) : showNotification(message['aps']['alert']);
   
      return;
    }, onResume: (Map<String, dynamic> message) {
      print('onResume: $message');
      
      return;
    }, onLaunch: (Map<String, dynamic> message) {
      print('onLaunch: $message');
     
      return;
    });

您可以使用以下代码访问数据:

 if (message.containsKey('data')) {
    // Handle data message
    final dynamic data = message['data'];
 }

拿到数据图后,就可以解析了。

https://pub.dev/packages/firebase_messaging展示了一个处理数据消息的例子,检查标题部分

  1. 定义一个 TOP-LEVEL 或 STATIC 函数来处理后台消息