I am trying to update an already delivered notification on iOS 10 which is a new feature. I am using an api call to send a push notification through firebase.
If a comes online notification should be "a is online"
If b also comes online notification's message should be updated to "a and b are online"
If c also comes online notification's message should be updated to "a,b and c are online"
and so on..
I have used answer from this question How can I removed previously delivered notifications when a new notification arrives with UNUserNotificationCenterDelegate in iOS 10?.
Whenever my backend sends a notification I wrote a logic in willPresentNotification to get the exact message needed and update an existing notification message.
But it only works only when the app is in foreground or background. If app is terminated then there is nothing in the frontend that can get executed to update the notification message. So every time backend sends a notification
I get "a is online", "b is online" and "c is online" all separate notifications.
So, how to get the desired behaviour from backend?? Do I have to save the identifiers of the notifications that I push, in the db and use them to update the message remotely?? Does iOS even allow a backend server to get the notification details from notification centre of a remote device?? If we use the identifier from backend will it update the message accordingly??
Thank you
Each time you create an updated notification...just use the same identifier
. For more see this moment for the WWDC video.
The above answer is for local notifications but you're looking for remote notifications...
Still see the same moment to get the idea...the only difference is that for local notifications you have identifier
...for remote notifications you have apns-collapse-id
header:
So just give a value to apns-collapse-id
and each time you send a new one it will update the previous one. Obviously if the user is in the app then you're out of luck, because they've already received the notification. This would only work if the user hasn't opened the notification yet (nor is in the app). If they have opened it then a new notification is sent.
apns-collapse-id
Multiple notifications with the same collapse identifier
are displayed
to the user as a single notification. The value of this key must not
exceed 64 bytes. For more information, see Quality of Service,
Store-and-Forward, and Coalesced Notifications.
From Apple docs:
To allow the coalescing* of similar notifications, you can include a
collapse identifier within a notification request. Normally, when a
device is online, each notification request that you send to APNs
results in a notification delivered to the device. However, when the
apns-collapse-id key is present in your HTTP/2 request header, APNs
coalesces requests whose value for that key is the same. For example,
a news service that sends the same headline twice could use the same
collapse identifier value for both requests. APNs would then coalesce
the two requests into a single notification for delivery to the
device. For details on the apns-collapse-id
key.
*: come together and form one mass whole