Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNUserNotificationCenter doesn't club together the notifications even after setting the same thread identifier to the UNMutableNotificationContent

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?

like image 850
AjinkyaSharma Avatar asked Oct 29 '22 21:10

AjinkyaSharma


1 Answers

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.

like image 74
Joseph Razon IL Avatar answered Nov 15 '22 07:11

Joseph Razon IL