9

我正在安排本地通知。它适用于 iOS 9.x,但从 iOS 10 开始

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

应用在 iOS 10 上运行时不会被调用。

我知道 iOS 已经引入了新UserNotifications框架,但这不应该停止工作 iOS 9 API。

我该如何解决这个问题?

4

1 回答 1

7

如您所知,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:didReceiveLocalNotificationapplication:didReceiveRemoteNotification:fetchCompletionHandler,解决方案很简单:只是不要将任何委托设置UNUserNotificationCenter.

请注意,即使您设置了委托,静默远程通知(包含content-available密钥和 no alertsound或的那些badge)始终由 处理。application:didReceiveRemoteNotification:fetchCompletionHandler

于 2017-04-11T16:07:22.050 回答