Search code examples
firebasefirebase-cloud-messagingfirebase-admin

How do I set "collapse_key" in Firebase Admin.messaging() SDK?


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

Here's the 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),
    }
};

Error: Messaging payload contains an invalid "collapse_key" property. Valid properties are "data" and "notification".

Do I need to use REST API for this? If so, that's really bad, because I have to pay extra for outgoing requests...


Solution

  • collapseKey is a property of MessagingOptions .You pass the options as the third parameter of 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);
      });
    

    A complete example is in the documentation.