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

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

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

我尝试在 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 放在哪里.云函数示例也没有显示这一点。我也找不到带有代码的文档

最佳答案

我建议使用最新的 send来自 firebase-admin 的函数lib,用法描述 here .
它似乎工作得很好,而且更容易应用。
带有 android/ios 特定 header 的消息示例:

// 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));

关于ios - 如何在 FCM 中使用 apns-collapse-id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61119466/

相关文章:

ios - 此假通知应转发到 firebase Swift5 iOS

ios - 如何在我的 iOS 应用程序中播放 vimeo 私有(private)视频

reactjs - 处理前端的后端错误状态

android - 应用程序是否需要对在 FCM 中使用和不使用 delivery_receipt_requested 收到的消息采取不同的操作?

android - 是否可以让多个服务器共享一个 Firebase Cloud Messaging 服务器 key ?

ios - removeFromSuperview() 突然不再工作

iOS - 是否可以将标签/ TextView 重置为其在 Interface Builder 中定义的初始状态?

node.js - 如何使用firebase云函数http触发器从firebase数据库获取数据

java - 小组回合制游戏

android - FirebaseInstanceIdService onTokenRefresh() 是否仅在应用程序运行时调用?