I am generating local notifications for iOS 10. The following is my sample code.
UNUserNotificationCenter *nCentre = [UNUserNotificationCenter currentNotificationCenter];
[nCentre requestAuthorizationWithOptions:(UNAuthorizationOptionBadge +
UNAuthorizationOptionAlert +
UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"Title";
content.subtitle = @"Subtitle";
content.body = @"Body Message";
content.categoryIdentifier = kCategoryOne;
content.threadIdentifier = @"123";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:kNotiIdOne
content:content
trigger:[UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5.0f
repeats:false]];
[nCentre addNotificationRequest:request
withCompletionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"error: %@",error.description);
}
}];
[nCentre setDelegate:self];
UNMutableNotificationContent *content2 = [[UNMutableNotificationContent alloc] init];
content2.title = @"Title";
content2.subtitle = @"Subtitle";
content2.body = @"Body Message";
content2.categoryIdentifier = kCategoryTwo;
content2.threadIdentifier = @"123";
UNNotificationRequest *request2 = [UNNotificationRequest requestWithIdentifier:kNotiIdTwo
content:content2
trigger:[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.0f repeats:false]];
[nCentre addNotificationRequest:request2 withCompletionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"error: %@",error.description);
}
}];
UNMutableNotificationContent *content3 = [[UNMutableNotificationContent alloc] init];
content3.title = @"Title";
content3.subtitle = @"Subtitle";
content3.body = @"Body Message";
content3.categoryIdentifier = kCategoryThree;
content3.threadIdentifier = @"123";
UNNotificationRequest *request3 = [UNNotificationRequest requestWithIdentifier:kNotiIdThree
content:content3
trigger:[UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5.0f
repeats:false]];
[nCentre addNotificationRequest:request3
withCompletionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"error: %@",error.description);
}
}];
I add 7 such notifications.
But the notifications in the notification centre do not group together. Is something missing from the code?
This solution worked for me;
First of all, I assured that threadIdentifier is the same:
if (@available(iOS 11.0, *)) {
UNTextInputNotificationAction *action = [UNTextInputNotificationAction actionWithIdentifier:@"Reply" title:@"Reply" options:UNNotificationActionOptionNone textInputButtonTitle:@"Send" textInputPlaceholder:@""];
UNNotificationCategory *showTitleCategory = [UNNotificationCategory categoryWithIdentifier:@"actionCategory" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionHiddenPreviewsShowTitle];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:showTitleCategory]];
content.threadIdentifier = @"LocalNotifications";
content.categoryIdentifier = @"actionCategory";
}
Second thing, keep the requestIdentifier unique, which in my case I did:
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:*someRandomString* content:content trigger:nil];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"add NotificationRequest succeeded!");
}
}];
And this is it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With