使用UNUserNotificationCenter后未调用didReceiveLocalNotification
我正在安排本地通知。 它适用于iOS 9.x但是从iOS 10开始
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
应用程序在iOS 10上运行时不会调用。
我知道iOS已经引入了新的UserNotifications
框架,但不应该停止使用iOS 9 API。
我该如何解决这个问题?
如您所知,iOS 10引入了UNUserNotifications
框架来处理本地和远程通知。 使用此框架,您可以设置委托以检测何时显示或点击通知。
[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(); }
现在,如果您仍想使用旧的(已弃用的) application:didReceiveLocalNotification
application:didReceiveRemoteNotification:fetchCompletionHandler
和application:didReceiveRemoteNotification:fetchCompletionHandler
,解决方案很简单:只是不要将任何委托设置为UNUserNotificationCenter
。
请注意,静默远程通知(包含content-available
密钥且没有alert
, sound
或badge
alert
)始终由application:didReceiveRemoteNotification:fetchCompletionHandler
处理application:didReceiveRemoteNotification:fetchCompletionHandler
,即使您已设置委托。