Notification service extension - hide previous notifications
Asked Answered
E

1

2

Take, for instance, a messaging app that only presents notifications with in this format: You have X new messages

When the user receives a message, the app will present a notification: You have 1 new message

When it receives a second message, a new notification will be shown:You have 2 new messages

Ideally, after receiving the second notification, the first one should be cancelled/hidden, as the user has only 2 new messages, instead of 1 + 2.

If notifications are locally presented using UserNotifications, to fix that you simply have to use the same identifier when creating the UNNotificationRequest for both notifications.

Is there a way to achieve the same when using a Notification Service App Extension?

Emulate answered 19/12, 2019 at 13:25 Comment(0)
E
1

This can be done by modifying the notification in the server side. There's a header named apns-collapse-id that you can set. More info on how to do so is available in this article by Apple.

All notifications with the same header will be coalesced - that is, only the newest one will be shown, the previous will disappear.

Emulate answered 19/12, 2019 at 17:5 Comment(4)
This is interesting. I have another use case, e.g. a simple calling app (no VoIP push notifications is used). Let's say there are 2 notifications: 'invite' when call, and 'leave' when hangup. What if User A called to User B and then User A canceled a call, so User B should get a push on invite, and then on leave - it should disappear. As we can not hide a previous push, we can replace it with leave using collapse id. Is this the only possible solution here?Ammamaria
@Ammamaria I believe so. Silent pushes could work for hiding notification, but are too unreliable for calls. If you can’t use VoIP pushes, then sending a new notification to replace the old ones with a message like “Missed call” is the best option available.Emulate
Hi @pepsy, can I set apns-collapse-id in Notification Service Extension when I receive notification, instead of setting it on server?Hexane
@TiếnNguyễnHữu I don’t think that’s possibleEmulate

© 2022 - 2024 — McMap. All rights reserved.