首页 > 解决方案 > Firebase 云消息通知模型

问题描述

我已经在我的颤振应用程序中使用 firebase_messaging 插件实现了 Firebase 云消息传递,但有些东西我不明白。我在函数中有这个有效负载,可以在创建的消息上发送推送通知:

               var payload = {
                                notification: {
                                      title: 'Nuevo mensaje!',
                                      body: `${sender} te ha dejado mensaje ${fecha}`,
                                      icon: 'https://*************.es/wp-content/uploads/2019/11/**************.png',
                                      click_action: 'FLUTTER_NOTIFICATION_CLICK'
                                       },
                             };

一切正常,直到我尝试以这种方式获得标题和正文:

onMessage: (Map<String, dynamic> message) async {
            print("onMessage: $message");
            showDialog(
                context: context,
                builder: (context) => AlertDialog(
                        content: ListTile(
                        title: Text(message['notification']['title']),
                        subtitle: Text(message['notification']['body']),
                        ),
                        actions: <Widget>[
                        FlatButton(
                            child: Text('Ok'),
                            onPressed: () => Navigator.of(context).pop(),
                        ),
                    ],
                ),
            );

它们是空的,所以我查看了这条消息,发现我收到了这样的消息:

{gcm.message_id: dsadsadasd, google.c.sender.id: dasdsaddcwfewf3, google.cae: 1, aps: {alert: {title:Nuevo mensaje!, body: Ronaldo te ha dejado mensaje: 7 3 2020 15:00} ,类别:FLUTTER_NOTIFICATION_CLICK}}

所以改变我解码消息的方式,比如:

content: ListTile(
            title: Text(message['aps']['alert']['title']),
            subtitle: Text(message['aps']['alert']['body']),
          ),

一切正常,但我想知道为什么我发送“通知:{title:}”但收到 aps:{alert:{title:}}。我遵循的教程似乎可以通过“通知”键正常接收。这里发生了什么?我错过了什么?该代码有效,但我觉得我没有以正确的方式实现它。

编辑:我刚刚在 Android 上对其进行了测试,并在那里解码了它应该做的方式:

 title: Text(message['notification']['title']),
 subtitle: Text(message['notification']['body']

所以现在我像 Platform.isAndroid 一样处理它?消息:消息;但我想知道发生了什么。

标签: node.jsflutterfirebase-cloud-messaging

解决方案


不确定这是否是您的问题的原因,但在我的云功能(应用程序中应该相同)中,我使用的有效负载结构在数据部分具有 click_action k:v 对:

const payload = {
        notification: {
          title_loc_key: "notification_title_string",
          body_loc_key: "notification_message_favoriting_string",
          badge: "1",
          sound: "default",
        },
        data: {
          click_action: "FLUTTER_NOTIFICATION_CLICK",
          rcpntId: recipientId,
          sndrId: senderId, 
        }
      };

我认为您需要将要通过警报对话处理的数据放入数据块中。通知块是关于设备状态栏中显示的内容。您还需要数据块用于 onResume 和 onLaunch 对通知的响应。


推荐阅读