iOS10如何接收后台通知?

How to receive the background notification in iOS 10?

描述 - Uptil iOS 9,调用以下方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

但是从 iOS 10 开始,苹果提供了新的框架,即 userNotification.framework 和新的处理方法,即

// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.

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

// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.

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

但是现在当应用程序处于后台且用户未点击通知时,我无法收到通知回调。

我想要的是当用户在后台时,如果他收到任何通知,那么我将通过哪种方法获得收到通知的回调。

提前致谢。

在iOS10中,willPresent将在应用运行前台时被调用。 didReceive 将在应用处于 运行 后台时用户点击通知时调用。

如果你想在应用程序处于运行后台时处理通知,你仍然需要实现didReceiveRemoteNotification...fetchCompletionHandler,并且不要忘记添加"content-available" 到您的通知负载。将其设置为 1。