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.