I need local notifications to display in foreground. I read that this is possible in the introduced in iOS10 UserNotifications framework. I try to implement it and get the notifications only in background. In foreground -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification gets called but the view of the notification is not shown. I would like it to show on both foreground and background, to be tappable and to trigger a method when tapped. Is this possible and what am I missing in my code if yes. This is the code: In AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound; [center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!granted) { NSLog(@"Something went wrong"); } }]; return YES; } -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSLog(@"Will present notification"); } -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { NSLog(@"Did Receive Notification Response"); } In a ViewController:
- (void)locationManagerDidEnterRegionOfCamera:(NSString *)cameraName { NSDictionary *info = @{ kRegionNotificationDictionaryCameraName : cameraName }; // Check if the switch was previously set to off and not fire notification if ([[NSUserDefaults standardUserDefaults] boolForKey:@"switch"]) { if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0.0")) { UNMutableNotificationContent *content = [UNMutableNotificationContent new]; content.title = @"Don't forget"; content.body = @"Buy some milk"; content.sound = [UNNotificationSound defaultSound]; UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO]; NSString *identifier = @"UYLLocalNotification"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Something went wrong: %@",error); } else { } }]; } else { [NotificationsHelper scheduleRegionNotificationWithInfo:info]; } } }
completionHandler(some UNNotificationPresentationOptionsValueAccordingToTheDoc)at the end ofuserNotificationCenter:willPresentNotification: withCompletionHandler:. Calling this block will allow you to decide to show it or not.