方法一:ios(3.0, 10.0)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
-
app运行时,调用方法一处理远程通知
-
如果远程推送到达时APP没有运行,方法一启动APP并且在launch选项字典中提供相应的信息。APP不会调用这个方法来处理远程通知。而是在application:willFinishLaunchingWithOptions:或者application:didFinishLaunchingWithOptions:的实现中获取远程通知装载的数据并且做出对应的响应。
方法二:ios(7.0)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
-
如果方法二实现了,系统会替代方法一调用方法二。与方法一只在APP运行在前台的时候才调用不同,方法二在APP运行在前台和后台时都会调用。并且,如果开启了后台通知模式,当接收到远程通知时,系统启动APP(后者从挂起状态恢复)并且将它置为后台状态。不过,如果用户强制退出了APP,系统不会自动启动app。这种情况下,用户必须在系统再次尝试自动启动你的APP之前重启APP或者重启设备。
-
如果程序因为远程通知启动或者恢复了,这个方法也会调用。各个代理方法先被调用。注意,相比之下,application:didReceiveRemoteNotification:方法,,如果实现了这个方法不会被调用,也不会调用This method will be invoked even if the application was launched or resumed because of the remote notification. The respective delegate methods will be invoked first. Note that this behavior is in contrast to application:didReceiveRemoteNotification:, which is not called in those cases, and which will not be invoked if this method is implemented.
-
如果用户从系统展示弹框中打开了APP,当APP将要进入前台时系统可能再次调用这个方法,如此你可以更新用户界面并且展示和通知相关的信息。
-
当接收到远程通知时,系统展示通知给用户并且为了可以调用这个方法在后台启动app(如果需要的话)。在后台启动APP给了你处理通知或者下载与其相关数据的时间,缩短通知到达与展示数据给用户之间的时间。
-
一旦你完成通知的处理,必须调用handle参数的块,否则app将会被终止。app最多有30秒时间来处理通知,并且调用特定的完成处理块。实际应用中,要在处理完通知后尽快调用handler块。系统会跟踪消耗的时间、电量和APP后台下载的数据。当处理远程通知时使用大量功耗的APP可能不会早早的唤醒来处理后来的通知。
方法三:ios(10.0)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
-
当APP在前台运行时,请求代理如何处理接收到的通知
-
只有APP在前台时代理才会调用这个方法。如果方法没有实现或者没有在时间规定内调用handler,通知不会被展示。应用程序可以选择以声音、角标、警报和/或通知列表的形式显示通知。这个选择应该以通知中的是否对用户展示的信息为基础。
-
如果代理没有实现这个方法,系统的表现会和你给completion传了UNNotificationPresentationOptionNone值一样,不展示。如果你没有为UNUserNotificationCenter对象提供代理,系统使用通知原来的选项来提示用户。
方法四:ios(10.0)- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
-
请求代理处理用户对发送的通知的响应
-
如果用户选择了app自定义动作,response参数包含这个动作的标识符(响应也能指示用户划掉了通知界面,或者没有选择自定义动作启动了APP)。在实现最后,调用completion handler块来通知系统已经处理完了用户的响应。如果没有实现这个方法,APP不会响应自定义动作。
-
在APP启动时使用UNNotificationCategory对象指定app的通知类型,使用UNNotificationAction对象指定自定动作的每个类型。