I have an app that alerts the user every x minutes via UNNotification local notification. If the user does not respond, the iOS lock screen shows a series of banners asking for user response. Most of the time, a user tap on the banner (after a tap on the Home button) triggers the UNNotification center delegate when the app is in background. However, occasionally a user tap on the latest banner does NOT trigger the delegate. Note: this is NOT a question about the delegate not receiving a call WITHOUT a user tap: I know this can't be done. Why would the iOS not trigger the app delegate once in a while when the user taps on the action button in the banner? Note: I keep track of the number of pending local notifications and never exceed the system limit of 64.
App delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
if(granted == YES){
[storage setBool:YES forKey:@"permission granted"];
[storage setBool:YES forKey:@"alert permission granted"];
[storage setBool:YES forKey:@"sound permission granted"];
}else{
NSLog(@"No permission granted");
[storage setBool:NO forKey:@"permission granted"];
};
}];
}
#pragma mark UNNotificationCenter setup
UNNotificationAction *acceptAction = [UNNotificationAction actionWithIdentifier:@"ACCEPT_IDENTIFIER" title:NSLocalizedString(@"Continue notifications", nil) options:UNNotificationActionOptionAuthenticationRequired];
UNNotificationAction *declineAction = [UNNotificationAction actionWithIdentifier:@"DECLINE_IDENTIFIER" title:NSLocalizedString(@"Stop notifications", nil) options:UNNotificationActionOptionAuthenticationRequired];
UNNotificationAction *doNotDisturbAction = [UNNotificationAction actionWithIdentifier:@"DO_NOT_DISTURB_IDENTIFIER" title:NSLocalizedString(@"Start Do Not Disturb", nil) options:UNNotificationActionOptionAuthenticationRequired];
NSArray *actions = [NSArray arrayWithObjects:acceptAction, declineAction, doNotDisturbAction, nil];
// NSArray *intentIdentifiers = [NSArray arrayWithObjects:@"none", nil];
UNNotificationCategory *invite = [UNNotificationCategory categoryWithIdentifier:@"com.nelsoncapes.localNotification" actions:actions intentIdentifiers: @[] options:UNNotificationCategoryOptionNone];
NSSet *categories = [NSSet setWithObjects:invite, nil];
[center setNotificationCategories:categories];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
#pragma mark UNNotification received in background
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"notification settings were changed");
NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
[storage setBool:YES forKey:KEventLoggerEventNotificationSettingsChanged];
[storage synchronize];
if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {
// Notifications not allowed
NSLog(@"notification settings were changed");
item.eventDescription = KEventLoggerEventNotificationsNotAllowed;
// check settings for alert and sound
}
// UNNotificationSetting alertSetting = settings.alertSetting;
if(settings.alertSetting == UNNotificationSettingEnabled){
[storage setBool:YES forKey:@"alert permission granted"];
item.eventDescription = KEventLoggerEventAlertsAreAllowed;
}else{[storage setBool:NO forKey:@"alert permission granted"];
item.eventDescription = KEventLoggerEventAlertsAreNotAllowed;
}
if (settings.soundSetting == UNNotificationSettingEnabled){
[storage setBool:YES forKey:@"sound permission granted"];
item.eventDescription = KEventLoggerEventSoundsAreAllowed;
}else {[storage setBool:NO forKey:@"sound permission granted"];
item.eventDescription = KEventLoggerEventSoundsAreNotAllowed;
}
}];
NSLog(@"appdelegate - center didReceiveNotificationResponse");
UNNotification *notification = response.notification;
if([actionIdentifier isEqual:@"com.apple.UNNotificationDefaultActionIdentifier"] || [actionIdentifier isEqual:@"com.apple.UNNotificationDismissActionIdentifier"]){
}else{
BOOL accept = [actionIdentifier isEqual:@"ACCEPT_IDENTIFIER"];
BOOL stop = [actionIdentifier isEqual:@"DECLINE_IDENTIFIER"];
BOOL doNotDisturb = [actionIdentifier isEqual:@"DO_NOT_DISTURB_IDENTIFIER"];
if (accept){NSLog(@"accept");
[self handleAcceptActionWithNotification:notification];
}
else if (stop){NSLog(@"stop");
[self handleDeclineActionWithNotification:notification];
}
else if(doNotDisturb) {NSLog(@"do not disturb");
[self handleDoNotDisturbActionWithNotification:notification];
};
}
View Controller:
-(UNNotificationRequest *)triggerNotifications: (NSString *)identifier : (NSTimeInterval) interval{
// Note: identifier must be unique or else each new request causes all others to be cancelled.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = NSLocalizedString(@"Timer expired", nil);
content.body = NSLocalizedString(@"Touch to continue", nil);
NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
BOOL sound = [storage boolForKey:@"sound permission granted"];
if(sound){
if([self.selectedSound isEqual:NSLocalizedString(kselectedSoundKeyDoorBell, nil)]){
content.sound = [UNNotificationSound soundNamed:@"doorbell.caf"];
}else if ([self.selectedSound isEqual:NSLocalizedString(kselectedSoundKeySystemDefault, nil)]){
content.sound = [UNNotificationSound defaultSound];
}else if ([self.selectedSound isEqual:NSLocalizedString(kselectedSoundKeyElectronicChime, nil)]){
content.sound = [UNNotificationSound soundNamed:@"electronic_chime.caf"];
}else{
if([self.selectedSound isEqual:NSLocalizedString(kselectedSoundKeyComputer, nil)]){
content.sound = [UNNotificationSound soundNamed:@"Computer.caf"];
}
}
}
content.categoryIdentifier = @"com.nelsoncapes.localNotification";
NSDate *today = [NSDate date];
NSDate *fireDate = [today dateByAddingTimeInterval:interval];
// first extract the various components of the date
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger year = [calendar component:NSCalendarUnitYear fromDate:fireDate];
NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:fireDate];
NSInteger day = [calendar component:NSCalendarUnitDay fromDate:fireDate];
NSInteger hour = [calendar component:NSCalendarUnitHour fromDate:fireDate];
NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:fireDate];
NSDateComponents *components = [[NSDateComponents alloc]init];
components.year = year;
components.month = month;
components.day = day;
components.hour = hour;
components.minute = minute;
// construct a calendarnotification trigger and add it to the system
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError *error){
if(error){
NSLog(@"error on trigger notification %@", error);
}
}];
}
I believe I found the problem (and it is in my code). I was calling triggerNotification twice with the same date components (I am only interested in the minute granularity because I use it to trigger an alert at the xth minute). The first call was when the user pressed a start button and the second call was when applicationDidBecomeActive was called in the app delegate. In fact, I did see duplicate notifications in my table. I think, but can't show, that when the system has two UNNotificationRequests pending for the same date components, it only responds to one of the corresponding alert banners. So, when I tapped the latest banner, the delegate was not called. I removed the second trigger and the problem appears to be resolved (i.e., tapping on a button in the banner results in a call to my delegate). Note: even though I was triggering a notification twice for the same date components, I did provide a unique identifier in each request).