9

I am scheduling local notifications. It works in iOS 9.x but since iOS 10

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

doesn't get called when app is running on iOS 10.

I know that iOS has introduced new UserNotifications framework but that shouldn't stop working iOS 9 APIs.

How can I resolve this issue?

jscs
  • 63,694
  • 13
  • 151
  • 195
user1947894
  • 123
  • 1
  • 6
  • Possible duplicate of [UILocalNotification is deprecated in iOS10](http://stackoverflow.com/questions/37938771/uilocalnotification-is-deprecated-in-ios10) – pedrouan Sep 02 '16 at 21:55
  • @pedrouan: I have already tried the solution given in http://stackoverflow.com/questions/37938771/uilocalnotification-is-deprecated-in-ios10. But it's not working. – user1947894 Sep 06 '16 at 16:26
  • I have the same problem with didReceiveLocalNotification stopped firing in iOS 10.0 and 10.1 GA. Looks like it's not just deprecated, it's not supported anymore unfortunately. – Billy Oct 31 '16 at 21:48

1 Answers1

7

As you know, iOS 10 introduced the UNUserNotifications framework to handle both local and remote notifications. Using this framework, you can set a delegate to detect when a notification is presented or tapped.

[UNUserNotificationCenter currentNotificationCenter].delegate = yourDelegate;

...

// In your delegate ...

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

    // Notification arrived while the app was in foreground

    completionHandler(UNNotificationPresentationOptionAlert);
    // This argument will make the notification appear in foreground
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler {

    // Notification was tapped.

    completionHandler();
}

Now, if you still want to use the old (deprecated) application:didReceiveLocalNotification and application:didReceiveRemoteNotification:fetchCompletionHandler, the solution is simple: just don't set any delegate to UNUserNotificationCenter.

Note that silent remote notifications (those which contain the content-available key and no alert, sound, or badge) are always handled by application:didReceiveRemoteNotification:fetchCompletionHandler, even if you have set the delegate.

Fernando SA
  • 1,041
  • 1
  • 12
  • 20