const options = {
priority: 'high',
collapseKey: user_id
};
const deviceTokensPromise = db.ref('/users-fcm-tokens/' + user_id).once('value');
deviceTokensPromise.then(tokensSnapshot => {
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no device tokens to send to.');
}
const tokens = Object.keys(tokensSnapshot.val());
console.log(tokens);
console.log(payload);
return admin.messaging().sendToDevice(tokens, payload, options).then(response => {
console.log(response);
return removeInvalidFCMTokens(tokensSnapshot, response);
});
});
I have a collapse-Key field in my options.
When this code is ran, the iPhone receives multiple notifications, all on top of each other. I'd like to have most recent notification replace the previous ones.
For iOS:
Use
apns-collapse-id
see the docs.See the implementation of
apns-collapse-id
in the article:For Android:
Use a
tag
variable in your notification payload.Check out the "Delivery Options" section in Firebase's FCM Messages documentation.
Intuitively you might expect that the
apns-collapse-id
setting might go into theoptions
parameter passed into thesendToMessage
method you are using. However, this is not the case. Instead try patching it into thepayload
object, like this:This follows the
payload
format presented in the documentation linked above.Once you've constructed this patched payload don't forget to update
sendToDevice(tokens, payload, options)
tosendToDevice(tokens, patchedPayload, options)
.Hope this works out for you!
@Timex you can pass
same notification id
for all notifications with the samecollapse_id
. For this you need to implement your ownSendNotification method
.