Yes, you can set thread-id
with FCM, but it serves to group notifications.
What you actually need to replace old notifications by new ones is apns-collapse-id
Use the same thread-id
to visually group multiple notifications together.
Multiple notifications with the same thread-id
are grouped and appear as a stack. Tapping on the stack expands all notifications with the most recent one being on top.
There's a collapseKey
option which can be used to skip sending outdated notifications. If the device didn't receive a bunch of notifications and there's a newer one with the same key - only the latest will be pushed.
It seems this would also work to replace old notifications on Android, but not on iOS.
As per delivery options there's an apple specific key: apns-collapse-id
What's a collapsible message
A collapsible message is a message that may be replaced by a new message if it has yet to be delivered to the device.
Quoted from: Non-collapsible and collapsible messages
Setting apns-collapse-id
apns-collapse-id
can be set using the lower level messaging APIs - send
, sendAll
and sendMulticast
, where you can specify an apns
key
It's set in the apns.headers
const payload = {
tokens: [/*...*/],
data: {/*...*/},
notification: {
title: 'My Title',
body: 'My Content',
},
apns: {
headers: {
'apns-collapse-id': doc.id,
},
payload: {
aps: {
sound: 'default',
},
},
},
};
admin.messaging().sendMulticast(payload);
Setting thread-id
threadId
can be set using the lower level messaging APIs - send
, sendAll
and sendMulticast
, where you can specify an apns
key
It's set in the apns.payload.aps
:
const payload = {
tokens: [/*...*/],
data: {/*...*/},
notification: {
title: 'My Title',
body: 'My Content',
},
apns: {
payload: {
aps: {
threadId: doc.id,
sound: 'default',
},
},
},
};
admin.messaging().sendMulticast(payload);
threadId
behavior while offline
For me there are 2 cases:
Sending a few notifications results in the latest being received when the device regains connectivity
Be offline with existing (non dismissed) notification(s) for a given threadId
. Regaining connectivity would add the latest notification to the top of the stack
Setting collapseKey
Higher level messaging APIs like sendToDeivce cannot tweak apns
directly but can specify options
One of those options
is options.collapseKey
const tokens = [/*...*/];
const payload = {
data: {/*...*/},
notification: {
title: 'My Title',
body: 'My Content',
sound: 'default',
},
};
const options = {
collapseKey: doc.id,
}
const response = await admin.messaging().sendToDevice(
tokens,
payload,
options,
);