问题
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