【问题标题】:How to use apns-collapse-id with FCM?如何将 apns-collapse-id 与 FCM 一起使用?
【发布时间】:2020-04-09 10:51:40
【问题描述】:

我知道这个问题已经被问过很多次了,但没有一个答案对我有用。有些问题甚至没有答案。

我正在尝试在 ios 上捆绑或分组类似的通知。我正在使用 FCM Cloud Functions 来触发通知。

以下是我尝试过的方法

const payload = {
        notification: {
          title: "Your medical history is updated" + Math.random(),
          tag: "MedicalHistory",
        },
        data: {
          click_action: "FLUTTER_NOTIFICATION_CLICK",
          sound: "default",
          status: "done",
        },
      };

      const patchedPayload = Object.assign({}, payload, {
        apns: {
          headers: {
            "apns-collapse-id": "MedicalHistory",
          },
        },
      });

      const options = {
        priority: "high",
        collapseKey: "MedicalHistory",
      };


await admin
          .messaging()
          .sendToDevice("MY_TOKEN", patchedPayload, options);

上面的代码不起作用

const payload = {
        notification: {
          title: "Your medical history is updated" + Math.random(),
          tag: "MedicalHistory",
        },
        data: {
          click_action: "FLUTTER_NOTIFICATION_CLICK",
          sound: "default",
          status: "done",
        },
        apns: {
          headers: {
            "apns-collapse-id": "MedicalHistory",
          },
        },
      };



const options = {
            priority: "high",
            collapseKey: "MedicalHistory",
          };

 await admin
              .messaging()
              .sendToDevice("MY_TOKEN", payload, options);

这也不行。

我不明白将apns-collapse-id 放在哪里。云函数示例也没有显示这一点。我也找不到带有代码的文档

【问题讨论】:

  • 你能试着用collapse_key改变你的options参数collapseKey吗?我查看了您的第二个有效负载,您似乎将apns-collapse-id. 放在了正确的位置。
  • 也许 sendToDevice 函数给出了一个承诺,所以放入 .then 和 .catch 并尝试查找错误。在您的问题中包含日志

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


【解决方案1】:

我建议使用firebase-admin 库中最新的send 函数,用法描述为here

它似乎工作得很好,而且更容易应用。

带有 android/ios 特定标头的消息示例:

// common data for both platforms
const notification = {
 title: 'You liked!',
 body: 'You really liked something...',
}
const fcmToken = '...'

// android specific headers
const android = {
  collapseKey: '0'
}

// ios specific headers
const apns = {
  headers: {
    "apns-collapse-id": '0'
  }
}

// final message
const message = {
 token: fcmToken,
 notification: notification,
 android: android,
 apns: apns,
}

// send message
admin.messaging().send(message).catch(err => console.error(err));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2018-07-20
    相关资源
    最近更新 更多