How to group notifications like whatsapp on IOS

若如初见. 提交于 2021-02-10 05:35:29

问题


I have in my app some notifications that you recive with GCM but every notification show in one target so when you get 2 or 3 notifications it makes annoying.

How to group all notifications in one target for my app? I think that will be like android, i have to identify the notification with some ID but i did not find any information about it.

Thats the code that execute when the app is in background:

// [START ack_message_reception]
func application( application: UIApplication,
                  didReceiveRemoteNotification userInfo: [NSObject :AnyObject]) {
    print("Notification received: \(userInfo)")
    // This works only if the app started the GCM service
    GCMService.sharedInstance().appDidReceiveMessage(userInfo);
    // Handle the received message

    // [START_EXCLUDE]
    NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
                                                              userInfo: userInfo)
    // [END_EXCLUDE]
}

And the GCM message code is:

array( 'body' => 'Someone wants to practice with you !!!',
                                            "sound" => "default",
                                            "vibrate" => "1",
                                            "time_to_live" => "1"
                                    );

Thank you for your help.


回答1:


Multiple notifications with the same collapse identifier are displayed to the user as a single notification. This can be handled from server side not at client side. APNS header apns-collapse-id will be used to update previously sent notification.

Refer to this for further description:

Table 8-2APNs request headers

Apple Developer Guide




回答2:


I know this question was asked long ago but I am posting this answer it might help someone looking for the same solution. It is possible to group notifications at client side from iOS 12.

What you need to do is just to set one property and it will do all for you. Below is the explanation with example to do that.

UNMutableNotificationContent *content; // Set all the properties like title, body etc. Here I am just going to explain how you can group notifications.

// Set property to group notifications
content.threadIdentifier = @"your group identifier";

Explanation: We have a property named threadIdentifier to group notifications, you just need to set this identifier to different unique group identifiers and iOS will handle the rest. It will show all the notifications having same identifier as one group.

Example: If we consider WhatsApp example they group messages notifications based on message sender, so we can set message sender number/identifier as threadIdentifier that's it.

content.threadIdentifier = @"messageSenderNumber"

Here is reference to Apple's guide for Using Grouped Notifications



来源:https://stackoverflow.com/questions/38349779/how-to-group-notifications-like-whatsapp-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!