如何在 FCM 中使用 apns-collapse-id?

Nea*_*eat 6 apple-push-notifications ios firebase firebase-cloud-messaging

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

我尝试在 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);
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用

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);
Run Code Online (Sandbox Code Playgroud)

这也行不通。

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

eya*_*oli 4

send我建议使用库中的最新函数firebase-admin,用法如下所述。

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

带有 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));
Run Code Online (Sandbox Code Playgroud)