【问题标题】:Firebase Cloud Messaging notification modelFirebase 云消息通知模型
【发布时间】:2020-06-19 12:36:19
【问题描述】:

我已经在我的颤振应用程序中使用 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.c.a.e: 1, aps: {alert: {title:Nuevo mensaje!, body: 罗纳尔多 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.js flutter firebase-cloud-messaging


    【解决方案1】:

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

    【讨论】:

    • 嗯,我会试一试,但我认为我不需要处理数据,因为我订阅了主题,当收到消息时,我将用户发送到通知屏幕,他在那里获取来自 firebase 的数据,并且可以看到新消息。
    • 好吧,我试过了,因为我必须添加一些数据但它不起作用。我读到在名为 apns 的有效负载中有一个 iOS 平台特定字段,但仍然是通知:{ },字段是通用的,用于将标题和正文发送到两个平台,所以我不知道,为什么在 iOS 上你会收到它此应用程序/警报表单。
    • 好的,我在您编辑后才意识到您的问题与 iOS 有关。到目前为止,我只在 Android 上测试过它,并期待 iOS 测试的乐趣。因此,您对该问题的解决方案将对我有所帮助,非常感谢。如果我在浏览文档、Medium 帖子、SO、Flutter 和 Firebase 论坛等时遇到了这个原因,我会在这里发布。
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 2018-03-13
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多