Firebase云消息通知模型



我已经在我的flutter应用程序中使用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:dasdsaddcwfewlf3,google.c.a.e:1,aps:{alert:{title:新门沙!,body:罗纳尔多te ha dejado mensaje:7 3 2020 15:00},类别:flaft_NOTIFICATION_CLICK}}

因此更改我解码消息的方式,如:

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

一切都很好,但我想知道为什么我发送了"通知:{title:}",却收到了aps:{alert:{title:}}。我遵循的教程似乎可以通过"通知"键正常接收它。这是怎么回事?我错过了什么?代码是有效的,但我觉得我没有以正确的方式实现它。

编辑:我刚刚在安卓系统上测试了它,它按照预期的方式解码:

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

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

不确定这是否是您问题的原因,但在我的云功能中(在应用程序中应该相同(,我使用的有效负载结构在数据部分具有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对通知的响应的数据块。

最新更新