FCM/GCM grouping notifications by "tag" on iOS
Asked Answered
O

2

10

So, when sending a notification to an Android device you can give a tag property:

"notification": {
    "title": title,
    "body": message,
    "sound": sound,
    "tag": "STRING_TO_GROUP_NOTIFICATIONS_BY"
}

This groups notifications with the same tag together so that they don't make a mess of the users notifications when there are a lot and only shows the newest one.

This is really useful in say, a chat app, with multiple channels that receive lots of messages so you can group by channel and minimize the amount of noise in the user's notifications.

Anways...

Is there any way to do this with iOS?

Ovariectomy answered 5/4, 2017 at 2:17 Comment(1)
did you work out how to do it in the end. If so could you post codeLublin
S
9

Update: apns-collapse-id is already available for FCM v1:

FCM provides a specific set of delivery options for messages sent to Android devices, and allows for similar options on iOS and web. For example, "collapsible" message behavior is supported on Android via FCM's collapse_key, on iOS via apns-collapse-id, and on JavaScript/Web via Topic. For details, see descriptions in this section and related reference documentation


The tag parameter is currently only supported for Android (which you probably already know which is why you're looking for iOS) and there is currently no counterpart for it in iOS.

From my answer here:

In order to bundle notifications in iOS, you'll have to specify a thread-id:

Provide this key with a string value that represents the app-specific identifier for grouping notifications. The system groups notifications with the same thread identifier together in Notification Center and other system interfaces. For local notifications, this key corresponds to the threadIdentifier property of the UNNotificationContent object.

However, there is currently no parameter counterpart for thread-id in FCM. What you could try and do is make use of a data message payload and specify the thread-id as a custom key-value pair.

Some possibly helpful posts:

Savannasavannah answered 5/4, 2017 at 2:27 Comment(7)
I'm actually using APNS for iOS, forgot to mention / tag, so this looks great. Thanks. Will accept answer once I've actually implemented some stuff.Ovariectomy
Cool. Good luck. :) PS: Do add some code samples in your post if you managed to implement them. Might be useful for other someday. Cheers!Savannasavannah
You can use HTTP v1 API in FCM to include thread-id in apns payload. firebase.google.com/docs/reference/fcm/rest/v1/…Bearish
There is now - "apns-collapse-id"Leprechaun
Thanks @barak109 I'll add this info in.Savannasavannah
It would be great if you could add some example code?Lublin
apns-collapse-id is not relevant to the question. It does not group messages for chat purposes. From Firebase documentation, apns-collapse-id use-cause: When there is a newer message that renders an older, related message irrelevant to the client app, FCM replaces the older message.Grimsley
P
3

According to the documentation tag would not group but replace notifications.

AndroidNotification

tag - Identifier used to replace existing notifications in the notification drawer. If not specified, each request creates a new notification. If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.

The apns header serving the same purpose is apns-collapse-id

But the question body is describing grouping and to achieve that on iOS you can use thread-id - notifications with the same thread-id are stacked together with the latest on top

To specify thread-id using the REST API you should put the value under payload: apns.payload.aps.thread-id

While to specify an apns-collapse-id it should be added under headers: apns.headers.apns-collapse-id

APNS Config reference

There are some more details in this SO answer, describing thread-id, apns-collapse-id and collapsibleKey, what each does and sample usage

Prescott answered 1/3, 2022 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.