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

Viewed 3210
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...

2 Answers

For those looking for an updated solution using most recent firebase-admin SDK and the new send() method, here's how I built my notification:

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

Source: Package types

Related