Firebase 云消息传递未以正确格式发送 iOS 通知内容和服务扩展的 aps 负载

Firebase Cloud Messaging not sending aps payload in correct format for iOS Notification Content & Service Extensions

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

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

这里是遇到的 Firebase 远程通知负载(其中一些长 Google 数字经过匿名编辑:

{
    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;
}

"category" 和 "mutable-content" 似乎不在正确的位置。它们应该在 aps 负载中。

如何让这些选项出现在负载中,以便我的应用程序能够正确解析它并将其与内容和服务扩展连接?

首先,我要提到 FCM 有两种类型的消息负载。 notificationdata。请参阅此处 documentation

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

例如,在您的 post 中,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.[=51= 在 相同级别 ]
  • (这个我可能理解错了,但是)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"
    }
}

然后从您自己的应用服务器发送。您也可以通过 or cURL

"mutable-content should be "mutable_content"(firebase 服务器的关键字,作为 IOS 的可变内容发送)正如您在 post 中提到的,我想您遗漏了编辑中。

下面是一个示例,其中还包含发送到 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"
  }
}

更新 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

这对我有用 云函数 Node.js

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