didReceiveLocalNotification not getting called after using UNUserNotificationCenter

我的未来我决定 提交于 2019-11-28 00:28:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/39300865/didreceivelocalnotification-not-getting-called-after-using-unusernotificationcen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!