如何在iOS 10中接收后台通知?

时间:2016-12-02 10:15:30

标签: push-notification ios10 firebase-notifications

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

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

但是从iOS 10开始,apple提供了新的框架,即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

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

我想要的是当用户在后台时,如果他收到任何通知,那么我将以哪种方式收到收到通知的回电。

提前致谢。

1 个答案:

答案 0 :(得分:4)

在iOS10中,当应用运行前台时,将会调用 willPresent 。当用户在应用运行背景时点按通知时,系统会调用 didReceive

如果您希望在应用运行后台时处理通知,则仍需要实施 didReceiveRemoteNotification ... fetchCompletionHandler ,并且不要忘记添加“content-available”到你的通知有效载荷。将其设置为1。