While looking at scheduling notifications, we saw that we needed to provide a unique identifier to each notification that is scheduled. If a new notification is added with an existing identifier, the new notification will overwrite the existing notification, limiting the amount of similar and duplicate notifications in the Notification Center.
The preceding example describes what happens if a notification has already been delivered. However, this overwrite behavior is not limited to notifications that have already been delivered. You can also manipulate pending notifications in the same fashion. Imagine that we've scheduled a recurring notification notifying the user about a certain event every morning. A silly example might be to notify the user about the number of times they opened your app the day before.
To do this, all you need to do is schedule the same recurring notification over and over again with a different number associated to it. As long as the notification uses the same identifier, the pending notification will simply be replaced with the new one.
You can also remove delivered notifications by calling removeDeliveredNotifications(withIdentifiers:) on the notification center in your app. If your app allows the user to select the types of notification they want to receive, you could use this feature to remove all notifications of a certain type. That way, the user won't see the notifications anymore.
To update notifications that have been sent by your server, you simply push a new notification with the apns-collapse-id header set on the request that your server sends to APNS. If this header is present, the new notification will overwrite any existing notifications that have been pushed with the same apns-collapse-id.
Updating your notifications instead of pushing new ones all the time is a great alternative, that will avoid clutter in the Notification Center; more importantly, there won't be any duplicate, redundant, or outdated messages fighting for the user's attention.
Let's see how we can further improve the notification experience through custom actions.