iOS 下APNS推送处理函数详解
相比起Android,iOS在推送方面无疑惯例得更好。APNS(Apple Push Notification Service)是苹果公司提供的消息推送服务。其原理就是,第三方应用将要推送给用户的信息推送到苹果服务器,苹果服务器再通过统一的系统接口将这些信息推送到用户的手机上。如果对此不舍了解的朋友可以参见这篇文章:一步一步教你做ios 推送
本文着重叫在App端如何处理推送信息。主要涉及一下几个比较重要的函数,而这些函数都是AppDelegate类中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- 推送类型的注册:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationAlert];这行代码告诉了系统,该程序注册的推送消息类型,通常包括badge、声音以及alert通知。
- 处理程序没有启动时的推送消息:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 或者:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
中完成。但是如果用户点击推送通知的时候程序还没有被启动,这个时候以上两个函数都是接收不到用户的推送通知的,这个时候需要在application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*) launchOptions 函数里面进行处理。而推送消息的相关信息就存储在launchOptions这个字典里。具体参照如下代码:NSDictionary* pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]; if (pushInfo) { NSDictionary *apsInfo = [pushInfo objectForKey:@"aps"]; if(apsInfo) { //your code here } }
- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken & - (void)application:(UIApplication *)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError *)error
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo和
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
都是程序在运行过程中(无论当前程序处于前台还是后台)接收到推送消息的处理函数。根据苹果的官方文档,建议大家使用
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
因为前者在程序处于后台的时候是无法接收到推送信息的(经实测-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo其实可以接收到,不知道是怎么回事,希望大虾解疑)。另外就是-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 还有一个作用。根据苹果给出的文档,系统给出30s的时间对推送的消息进行处理,此后就会运行CompletionHandler程序块。
在处理这类推送消息(即程序被启动后接收到推送消息)的时候,通常会遇到这样的问题,就是当前的推送消息是当前程序正在前台运行时接收到的还是说是程序在后台运行,用户点击系统消息通知栏对应项进入程序时而接收到的?这个其实很简单,用下面的代码就可以解决:
void application:(UIApplication*)application didReceiveRemoteNotification:NSDictionary)userInfo fetchCompletionHandler:((^)UIBackgroundFetchResult)completionHandler{ if (application.applicationState == UIApplicationStateActive) { NSLog(@"active"); //程序当前正处于前台 } else if(application.applicationState == UIApplicationStateInactive) { NSLog(@"inactive"); //程序处于后台 } }
{ |
"aps" : { |
"alert" : "You got your emails.", |
"badge" : 9, |
"sound" : "bingbong.aiff" |
}, |
"acme1" : "bar", |
"acme2" : 42 |
} |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。