如何在Firebase Admin.messaging()SDK中设置“collapse_key”?

时间:2017-09-12 22:41:48

标签: firebase firebase-cloud-messaging firebase-admin

admin.messaging().sendToDevice(tokens, payload)

这是有效载荷:

const payload = {
    collapse_key: "something",
    notification: {
        body: message.body || "[ Sent you a photo ]",
    },
    data:{
        "thread_id": String(thread_id),
        "message_id": String(message_id),
        "user_id": String(message.user_id),
        "created_at": String(message.created_at),
    }
};

错误:消息传递有效内容包含无效的“collapse_key”属性。有效的属性是“数据”和“通知”。

我是否需要使用REST API?如果是这样,那真的很糟糕,因为我必须为传出请求支付额外费用......

1 个答案:

答案 0 :(得分:4)

collapseKeyMessagingOptions的属性。您将选项作为sendToDevice()的第三个参数传递。

const options = {
  priority: 'high',
  collapseKey: 'myCollapseKey'
};

admin.messaging().sendToDevice(tokens, payload, options)
  .then(function(response) {
    console.log("Successfully sent message:", response);
  })
  .catch(function(error) {
    console.log("Error sending message:", error);
  });

完整的示例位于the documentation