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...
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With