【问题标题】:How do I set "collapse_key" in Firebase Admin.messaging() SDK?如何在 Firebase Admin.messaging() SDK 中设置“collapse_key”?
【发布时间】:2017-09-12 22:41:48
【问题描述】:
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 吗?如果是这样,那真的很糟糕,因为我必须为发出的请求支付额外费用......

【问题讨论】:

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


    【解决方案1】:

    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

    【讨论】:

      【解决方案2】:

      对于那些使用最新的firebase-admin SDK 和新的send() 方法寻找更新解决方案的人,我是这样构建通知的:

      {
         "token": TARGET_DEVICE_FCM_TOKEN,
         "notification": {
            "title": "Notification title",
            "body": "Optional body",
         },
         "android": {
            "collapseKey": "YOUR_KEY",
         }
         ...rest of payload
      }
      

      来源:Package types

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-23
        • 2022-11-21
        • 1970-01-01
        • 2021-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多