16

From the documentation:

thread-id | string | When displaying notifications, the system visually groups notifications with the same thread identifier together. For remote notifications, the value of the threadIdentifier property is set to the value of this request header.

Our push notification payloads:

{
    aps =     {
        alert =         {
            body = "Leeroy J asked you: Test Push Notification";
        };
        badge = 12;
        sound = default;
        "thread-id" = 9150;
    };
    n = "6kQ/0x6556";
    r = 9150;
}

{
    aps =     {
        alert =         {
            body = "Leeroy J re: Test Push Notification";
        };
        badge = 13;
        sound = default;
        "thread-id" = 9150;
    };
    n = "6l8/0x6582";
    p = 7197;
    r = 9150;
}

Here's what it looks like in Message Center:

enter image description here

I expected them to be visually different than other notifications... either that or grouped together when other notifications come in between. In our testing, neither is the case.

What am I doing wrong? Or am I misunderstanding this feature?

mfaani
  • 33,269
  • 19
  • 164
  • 293
Albert Bori
  • 9,832
  • 10
  • 51
  • 78

5 Answers5

23

You don’t need:

  • apns-collapse-id
  • NotificationContentExtension

Never in the docs it's mentioned that way that you need them. It's just that they are related.

Solution:

For me it worked out of the box. It's just the iOS doesn't immediately render notifications into groups.

  1. Send Notifications with thread-ids
  2. Then Wait. Let your iPhone/iPad lock screen turn off. (Mine was locked).
  3. Then send another message
  4. If that doesn't work then just be patient a bit and try again.

Then all of the sudden it starts to group and work as expected!!!

For the stacking to happen you need a few notifications. If there is too much space, more likely on an iPad then it will need more to start stacking them. Also make sure you've correctly set your notification grouping setting to 'Automatic'

enter image description here

Other usage of thread-id:

Usage of thread-id has another purpose that is best explained within messaging apps. Think of iMessage.

enter image description here

  1. You get a message from your friend.
  2. You Long press the notification.
  3. It will open a pop where you can chat from within the contentExtension
  4. If your friend sends more messages, then your content-extension's didReceiveNotification will keep getting called. It gets called so you can update the conversation.

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 method is called on the main thread of your notification content app extension.

reference to didReceive(_:) docs

For more on this see this answer

Summary

There are two distinct use cases for thread-id:

  • Grouping notifications
  • Getting continuous callbacks to your contentExtension for a single thread-id

What is apns-collapse-id?

Used if you've sent a notification and it somewhat needs correction/update. Example you're WashingtonPost and someone has just won the presidency.

You first send a notification with just a title of 'Albert Bori has won 2020 election!!' with a body of: This message will be updated shortly...

Then 3 minutes later, you get more information that he won the elections by winning in 40 states and losing in 10. So you update the body. Then you get more information of exact number of votes. So you update the body again. I believe can constantly update the message without alerting (no sound, nor new banner. It would just get updated in the notification center) the user it was updated.

Only if the user has already dismissed/viewed the notification then a notification with the same apns-collapse-id will alert/notify the user. It's not for grouping it's for coalescing!

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • Unfortunately the grouping of messages is not working for me. Tried all sorts of possible configuration, etc. Any suggestions ? – NKM Jul 10 '20 at 09:56
  • @NKM create a question. Show your payload. Show your screen. Also include the iOS version. Before creating a question I might just try it on another device as well – mfaani Jul 10 '20 at 10:15
  • 1
    Update - I started seeing grouping of notifications after a few mins (doing lock and unlock of the screen). Though it still takes a few minutes for messages to group. – NKM Jul 17 '20 at 03:59
  • Since yours is the most elaborate answer, do you have any experience with the same - on MacOS? I'm struggling to find documentation about the differences, any sample-code at-all, and any information will be greatly appreciated. – Motti Shneor Apr 28 '21 at 20:46
  • I have no macOS experience, why not open a question and link it here. Ill take a look... – mfaani Apr 28 '21 at 21:01
  • Great write-up, thanks for info & explanations. – CybeX Oct 12 '22 at 07:28
5

Unfortunately, you don't get this for free. You need to create a Notifictation UI extension, and implement your own UI(in this case it's a conversation UI), and update the UI when receive new notification with same thread-id.

Edit: this may not be the correct answer, see a more comprehensive answer here.

Tony
  • 1,581
  • 11
  • 24
  • 1
    I added notification UI Extension. But as I am testing this feature, I find that in the notification Center I will get two different notifications even if my thread-id are same. Only when I chose to force-touch the notification my Notification UI Extension will be fired. This doesn't serve my purpose, as I want to actually group or even collapse all the notifications with same thread-id. Notification UI Extension doesn't allow to collapse notifications on the notification center ? am i right? – iSaalis May 31 '17 at 08:55
  • 3
    It's intended behaviour to load extension on force touch, you can't customise the notification cell displayed on lock screen and notifications screen. You can try putting [apns-collapse-id](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW13) into APNs request header. – Tony Jun 07 '17 at 11:40
  • Have you tested this out? Or you just read docs? See my answer for why I think your answer is incorrect. – mfaani Mar 04 '20 at 22:50
  • 1
    @Honey I answered this question a looong time ago, I don't remember what I did before answering, but your answer makes total sense to me, I've upvoted your answer and added a link to it in mine, I hope I can untick my answer so that I don't confuse people, but I don't think I'm able to. – Tony Jun 23 '20 at 08:52
2

Please note that the thread-id type must be a string and in your payload it's an integer. It works out of the box.

nandodelauni
  • 291
  • 1
  • 10
-1

I had this problem with Firebase not using apns-collapse-id. How I solved it was to use key instead of certificates. You can find the key right under the certificates in developer.apple.com . It now uses one "row" of notification and puts the last notification from group into that row. Advanced features such as replacing content would have the necessity to use notification ui extension.

Darko
  • 615
  • 7
  • 21
-2

https://medium.com/the-guardian-mobile-innovation-lab/how-to-replace-the-content-of-an-ios-notification-2d8d93766446:

says to use apns-collapse-id instead

Gerry
  • 1,031
  • 1
  • 14
  • 30
  • 4
    They're two different things. apns-collapse-id replaces the content of a single push. Effectively letting you overwrite it. Thread-id is using for grouping notifications together logically. Say if you have a chat app where you're getting push notifications in two different chat rooms. In notification center these will be grouped separately if you provide two different thread ids. – markquezada Jun 06 '19 at 07:54