【问题标题】:Firebase Cloud Messaging not sending aps payload in correct format for iOS Notification Content & Service ExtensionsFirebase Cloud Messaging 未以正确格式为 iOS 通知内容和服务扩展发送 aps 有效负载
【发布时间】:2017-08-23 07:13:59
【问题描述】:

我正在尝试使用 Firebase 实现通知。当应用程序在后台或前台时,通知被正确接收。因此,基本机制正在发挥作用。

现在我已经向应用程序添加了内容扩展和服务扩展。当我使用本地通知时,内容扩展有效,但就可选字段而言,Firebase 消息有效负载似乎不正确。这是我的控制台图像的链接:

这里是遇到的 Firebase 远程通知负载(为了匿名而编辑了一些长谷歌号码:

{
    aps = 
    {
        alert = 
        {
        body = "Eureka! 11";
        title = "Patient is not doing well";
        };
    };
    category = provider-body-panel;
    gcm.message_id = 0:149073;
    gcm.n.e = 1;
    google.c.a.c_id = 2825604;
    google.c.a.e = 1;
    google.c.a.ts = 149073;
    google.c.a.udt = 0;
    mutable-content = 1;
}

“类别”和“可变内容”似乎不在正确的位置。它们应该在 aps 有效载荷中。

如何让这些选项包含在有效负载中,以便我的应用可以正确解析它并将其与内容和服务扩展连接?

【问题讨论】:

    标签: ios firebase apple-push-notifications firebase-cloud-messaging


    【解决方案1】:

    首先,我要提到 FCM 有两种类型的消息有效负载。 notificationdata。在此处查看documentation

    通过 Firebase 通知控制台发送通知时,它将被视为 notification 有效负载。但是,如果您添加 自定义数据,它会将其作为自定义键值对添加到有效负载中。

    例如,在您的帖子中,FCM 有效负载应如下所示:

    {
        "notification": {
            "body" : "Eureka!",
            "title": "Patient is not doing well"
        },
    
        "data": {
            "category": "provider-body-panel",
            "mutable-content" : true,
            "click_action" : "provider-body-panel"
        }
    }
    

    怎么了?

    • click_action 应该在 notification 内。
    • mutable-content 应该是mutable_content(注意下划线)并且应该与notification同一级别
    • 这个我可能理解错了,但是)FCM没有category参数,click_action已经对应了。

    请参阅文档以获取参数here

    目前无法在使用 Firebase 通知控制台时设置 click_actionmutable_content 的值。您必须自己构建有效载荷,如下所示:

    {
        "to": "<REGISTRATION_TOKEN_HERE>",
        "mutable_content" : true,
        "notification": {
            "body" : "Eureka!",
            "title": "Patient is not doing well",
            "click_action" : "provider-body-panel"
        }
    }
    

    然后从您自己的应用服务器发送它。您也可以通过using PostmancURL 进行此操作

    【讨论】:

    • 谢谢!这与我在 Google 自己的页面上找到的关于在其中使用 aps 对象作为 notificationcategory 的兄弟姐妹的文档背道而驰。当然是经典的谷歌......你的就是那个有效的。非常感谢。
    【解决方案2】:

    "mutable-content 应该是"mutable_content"(firebase 服务器的关键字,作为 IOS 的可变内容发送),正如您在帖子中提到的,我认为您在编辑中遗漏了。

    下面是一个示例,其中包含发送到 FCM 服务器的 json 中数据部分的更正格式。 所以更新将是:

    { 
      "to" : "YOUR firebase messaging registration id here",
      "mutable_content":true,
      "notification": {
        "title": "Its about time",
         "body": "To go online Amigo",
         "click_action": "NotificationCategoryIdentifier ForYourNotificationActions"
      },
      "data":{
        "customKey":"custom data you want to appear in the message payload"
        "media-attachment":"mycustom image url",
        "catalogID":"mycustom catalog for my custom app"
      }
    }
    

    【讨论】:

      【解决方案3】:

      更新 Firebase Admin SDK 并使用 sendMulticast(payload) 方法

      var admin = require("firebase-admin")
      
      admin.initializeApp({
          credential: admin.credential.applicationDefault(),
      });
      
      // Create a list containing up to 500 registration tokens.
      // These registration tokens come from the client FCM SDKs.
      const registrationTokens = [
          'YOUR_REGISTRATION_TOKEN_1',
          // …
          'YOUR_REGISTRATION_TOKEN_N',
      ];
      
      // See documentation on defining a message payload.
      var message = {
          notification: {
              title: '$FooCorp up 1.43% on the day',
              body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
          },
          tokens: registrationTokens,
          apns: {
              payload: {
                  aps: {
                      'mutable-content': true,      // use single quote  
                      'category': 'INVITE_CATEGORY' // use single quote   
                  }
              },
          },
      };
      
      // Send a message to the device corresponding to the provided
      // registration tokens.
      admin.messaging().sendMulticast(message)
        .then((response) => {
          if (response.failureCount > 0) {
            const failedTokens = [];
            response.responses.forEach((resp, idx) => {
              if (!resp.success) {
                failedTokens.push(registrationTokens[idx]);
              }
            });
            console.log('List of tokens that caused failures: ' + failedTokens);
          }
        });
      

      参考:https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_specific_devices

      【讨论】:

        【解决方案4】:

        这对我有用 Node.js 的云功能

        const payload = {
            notification: {
                title: name,
                body: messageText,
                badge: "1",
                mutable_content: "true"
            },
            data: {
                type: "MESSAGE",
                fromUserId: name,
                attachmentUrl: imageUrl
            }};
        

        【讨论】:

          猜你喜欢
          • 2016-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-24
          • 1970-01-01
          • 1970-01-01
          • 2017-01-05
          相关资源
          最近更新 更多