5

TL;DR: What key needs to be set in the APNs notification payload JSON to correspond to the threadIdentifier property of the UNNotificationContent object? e.g. the "category" key corresponds to the categoryIdentifier property.


iOS 10 introduces the Notification Content Extension allowing us to present a view controller when a notification is expanded.

The view controller that we provide conforms to the UNNotificationContentExtension protocol, that requires us to implement the didReceive(_:) method.

The documentation for this method includes the following paragraph:

This method may be called multiple times while your view controller is visible. Specifically, it is called again when a new notification arrives whose threadIdentifier value matches the thread identifier of the notification already being displayed.

The threadIdentifier property may be set in code for local notifications, but I don't know how to set it for remote notifications that are sent from the server to APNs.

The UNNotificationContent documentation describes the property here: http://developer.apple.com/reference/usernotifications/unnotificationcontent

The following JSON includes the keys I've tried ("thread" and "thread-identifier"):

{
    "aps" : {
        "alert" : "Hello World!",
        "sound" : "default",
        "category" : "example-category",
        "thread" : "example-thread",
        "thread-identifier" : "example-thread-identifier"
    }
    "custom-field" : "some value",
}

I can't find any documentation from Apple about how to set this. Can anyone help?

Sam
  • 5,892
  • 1
  • 25
  • 27

1 Answers1

12

I discovered from a contact at Apple that the correct key to populate this property is the "thread-id" key.

So the JSON sent to APNs is as follows:

{
    "aps" : {
        "alert" : "Hello World!",
        "sound" : "default",
        "category" : "example-category",
        "thread-id" : "my conversation blah blah"
    }
    "custom-field" : "some value",
}

This populates the threadIdentifier property of the UNNotificationContent object accessible in your Notification Content Extension via notification.request.content.threadIdentifier.

By setting this "thread-id" value, it means that the didReceive(_:) method of your content extension will be multiple times. First when expanding the notification initially, and again whenever a new notification arrives with the same "thread-id" value.

I assume (hope) this will be added to the official documentation once iOS 10 is officially released.

Sam
  • 5,892
  • 1
  • 25
  • 27
  • https://medium.com/the-guardian-mobile-innovation-lab/how-to-replace-the-content-of-an-ios-notification-2d8d93766446 says to use apns-collapse-id – Gerry Jul 11 '18 at 19:14
  • yes, this post is from a long time ago. apns-collapse-id isn't a key in the aps JSON. it has to be sent in the HTTP Header – Sam Jul 11 '18 at 20:53
  • it's used to collapse notifications since iOS 10. but i believe (not sure) that the "thread-id" key should be used to group messages in iOS 12 (new grouping fctinlty) – Sam Jul 11 '18 at 20:55
  • should it work with https://fcm.googleapis.com/fcm/send in postman? I have set it there with a string and number but its still not working – Ross Rawlins Oct 11 '21 at 16:02