Setting Notification Identifier for Remote Notification

Hello - I have a working app with local notifications setting an identifier and updating a previous notification.


        let center = UNUserNotificationCenter.current()
     
        let content = UNMutableNotificationContent()
        content.title = "Updating Notification"
        content.body = "Updated Body Content"
        let requestIdentifier = "update.this.notification"  // Set an identifier value
     
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger) // Pass identifier to the request
     
        center.add(request) {(error) in print(error?.description)}
    


I also have a working UNNotificationServiceExtension that accepts a remote push and modifies its content before displaying to the user. Howevier I'm struggling to figure out how to set the identifier value from within the UNNotificationServiceExtension. The UNNotificationRequest object has a get-only identifier property.

Answered by benr75 in 152277022

In order to update a notification on a subsequent push you must set the apns-collapse-id value in the header using APNS HTTP/2. Not all push libraries have been updated for this and the library I was using did not have support for it. I added support and hopefully it will be merged in shortly to the Ruby Apnotic gem. https://github.com/ostinelli/apnotic/pull/19


This is now supported in the ruby apnotic gem. https://github.com/ostinelli/apnotic


Thanks to mungbeans for pointing me to the apns-collapse-id.

Why WOULD anybody need to change it once its set?

As information changes you can update a push moving it to the top of the list of notifications instead of stacking multiples. Imagine sports scores where you have an update every quarter, instead of four pushes filling the users notification they could have one push with the latest score in it.


If the ID can be set prior to entering the extension that also works, but I haven't been able to figure out a key/value to put into the push payload to do this either.

I don't ask why or how you could make use of the identifier, I asked why whould you or anybody ever need to change it once its already been set. You set it once thats it. It wouldn't make any sense to change it. It is read only intentionally to prevent people from changing it because to do so just doesn't make sense.

If its a remote notification you set the identifer in the push payload, then once its set you can access it in your app.

Great! Can you point me to how to set the identifier in the remote notification payload?


Not mentioned here (yes I know this is not beta documentation, but it is all I can find)

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW2

Add a value to the push payload with a name of 'apns-collapse-id', and whatever identifer you want to use.

Accepted Answer

In order to update a notification on a subsequent push you must set the apns-collapse-id value in the header using APNS HTTP/2. Not all push libraries have been updated for this and the library I was using did not have support for it. I added support and hopefully it will be merged in shortly to the Ruby Apnotic gem. https://github.com/ostinelli/apnotic/pull/19


This is now supported in the ruby apnotic gem. https://github.com/ostinelli/apnotic


Thanks to mungbeans for pointing me to the apns-collapse-id.

Hi,


At what level in the push payload does one add the apns-collapse-id key? Ie is it at top, under aps or under alert?


What is acceptable for its content? Ie can it be any string or does it have to be a uuid?


Thanks,

Chris

Setting Notification Identifier for Remote Notification
 
 
Q