3

I have declared my class to conform to UNUserNotificationCenterDelegate as well, I am declaring said class as the current UNNotificationCenter delegate in didFinishLaunchingWithOptions.

However, no matter what I do, I am unable to receive a callback to the method userNotificationCenter didReceiveNotificationResponse on my custom action. Code is attached below.

Things that work: - able to see the notification fine - able to click the button Launch app which does indeed, launch the app

Things that don't: - able to click the button Test which simply dismisses the notification, and it does not call the method userNotificationCenter didReceiveNotificationResponse

UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];

NSDate *now = [[NSDate alloc]init];
NSDate *inABit = [now dateByAddingTimeInterval:10];

NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:inABit];

UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];

//register category

UNNotificationAction *test = [UNNotificationAction actionWithIdentifier:@"test" title:@"Test" options:@[]];
UNNotificationAction *launchApp = [UNNotificationAction actionWithIdentifier:@"launchApp" title:@"Launch App" options:UNNotificationActionOptionForeground];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"notification" actions:@[test, launchApp] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
[notificationCenter setNotificationCategories:[NSSet setWithObject:category]];

//schedule notification
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
[content setTitle:@"Hello"];
[content setBody:@"Hello notification!"];
[content setCategoryIdentifier:@"notification"];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"request" content:content trigger:calendarTrigger];

[notificationCenter addNotificationRequest:request withCompletionHandler:
 ^(NSError *error){
     if(error!=nil)
     {
         NSLog(@"There was an error adding a notification request.");
     }
 }];

And of course, I have implemented the method (userNotificationCenter didReceiveNotificationResponse) I just did not want to show it here as it takes up space. The method does a NSLog and schedules another notification, 10 seconds later. All of which, do not occur as the method is not being called.

Thomas Oo
  • 379
  • 4
  • 14
  • It is an iOS bug. It has been fixed in iOS 10.1, thanks to [@Yongxiang Ruan](http://stackoverflow.com/a/39781105/2227031) – jk2K Oct 30 '16 at 03:05
  • might you have forgot to place this in .m file: `self.userNotificationCenter.delegate = self;` Hope it helps :) – Rishav Chudal Nov 21 '18 at 06:10

0 Answers0