TIMEX
TIMEX

Reputation: 272374

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

Upvotes: 3

Views: 4340

Answers (2)

Dante
Dante

Reputation: 93

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

Upvotes: 2

Bob Snyder
Bob Snyder

Reputation: 38319

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.

Upvotes: 7

Related Questions