码迷,mamicode.com
首页 > 移动开发 > 详细

iOS推送小结

时间:2015-08-12 11:36:55      阅读:2170      评论:0      收藏:0      [点我收藏+]

标签:ios   推送   

iOS推送小结

(吐槽,md的代码编辑功能不知道是不会用还是确实不好用)

1.推送配置

1.1证书配置

请自行谷百.

1.2注册推送

    //代码来源:环信Demo
    //In method application:(UIApplication *)application didFinishLaunchingWithOptions:

    UIApplication *application = [UIApplication sharedApplication];
//注册APNS
if([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
    UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
}

#if !TARGET_IPHONE_SIMULATOR
if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
    [application registerForRemoteNotifications];
}else{
    UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
    UIRemoteNotificationTypeSound |
    UIRemoteNotificationTypeAlert;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
}
#endif

registerUserNotificationSettings iOS8.0以后使用该方法来为app注册推送权限,第一次安装app时会弹出对话框提示用户是否允许接收推送消息.多次调用该方法来注册UIUserNotificationSettings会导致上一次的设置失效.

registerForRemoteNotifications iOS8.0以后在使用registerUserNotificationSettings方法注册推送权限后,还需要使用该方法开启远程推送权限(推送分为本地推送和远程推送,本地推送在此不做描述).该方法会回调application:didRegisterForRemoteNotificationsWithDeviceToken(在这个方法中需要将得到的deviceToken传给自己的推送服务器): 或者application:didFailToRegisterForRemoteNotificationsWithError: 两个方法之一.

**registerForRemoteNotifications**iOS3.0-iOS8.0以前注册推送的方法.

注意:上面的代码之所以判断是否为模拟器是因为模拟器是没有DeviceToken的,所以注册远程推送没有意义,推送原理会在文章末尾简介.

特别地,在iOS8中,在使用registerUserNotificationSettings:注册推送权限时增加了一个categories参数,可以用来自定义增加一些对推送消息的处理行为,示例代码如下:

 UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"acceptAction";
acceptAction.title = @"抢单";
acceptAction.activationMode = UIUserNotificationActivationModeForeground;

UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"rush";
NSArray *actions = @[acceptAction];
[categorys setActions:actions forContext:UIUserNotificationActionContextMinimal];

 UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:[NSSet setWithObjects:categorys, nil]];
    [application registerUserNotificationSettings:settings];

此外,在进行推送时,需要在推送消息内增加一个category的key,value为rush,这样客户端在收到消息后下拉或者在锁屏界面左滑会出现一个抢单按钮.

处理category消息可再appdelegate的如下方法中进行:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
    NSLog(@"%@----%@",identifier,userInfo);
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"抢单成功" message:nil
delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

    [alert show];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"rushSuccess" object:nil];

 //处理完消息,最后一定要调用这个代码块
 completionHandler();

}

1.3处理推送

如果是程序正在运行或者说程序正在后台,那么这个时候处理推送消息的工作都是在:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 

或者:

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

中完成。

如果应用程序没有启动:

  1. 带category的消息在点击相应的action后会直接进入application:(UIApplication *)application handleActionWithIdentifier:方法中进行处理,
  2. 不带category的消息在点击消息进入app后,会先调用application didReceiveRemoteNotification:fetchCompletionHandler:(iOS8实测中发现一直调用的是该方法,并未出现调用application didReceiveRemoteNotification的情况),之后会调用application didFinishLaunchingWithOptions:.

application didFinishLaunchingWithOptions:的launchOptions参数说明如下:

  1. 若用户直接启动,lauchOptions内无数据;
  2. 若由其他应用程序通过openURL:启动,则UIApplicationLaunchOptionsURLKey对应的对象为启动URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey对应启动的源应用程序的bundle ID (NSString);
  3. 若由本地通知启动,则UIApplicationLaunchOptionsLocalNotificationKey对应的是为启动应用程序的的本地通知对象(UILocalNotification);
  4. 若由远程通知启动,则UIApplicationLaunchOptionsRemoteNotificationKey对应的是启动应用程序的的远程通知信息userInfo(NSDictionary);
  5. 其他key还有UIApplicationLaunchOptionsAnnotationKey,UIApplicationLaunchOptionsLocationKey,
    UIApplicationLaunchOptionsNewsstandDownloadsKey。

2.APNs推送简介

每台iOS设备都会与APNs建立一个TLS连接,并凭此来接受推送消息.

如果设备处于断网离线状态,APNs会将要推送的消息缓存一小段时间,并在设备上线后推送消息.只能有一条时间最近的消息被缓存,如果在设备离线期间发送了多条消息,只会有时间上最后一条消息被推送.如果设备长期处于离线状态,那么所有缓存的离线消息都将被清空.

iOS8之后,推送消息的最大长度为2Kb,iOS8以前则为256bytes.

此外,还可以设置content-available字段来发送静默消息(用户界面不会有推送提示,将在之后发文介绍).

看了apple官方文档,发送推送的道道还挺多,预计将会是一系列的文章+示例进行说明.由于之后工作中会涉及到后台定位,所有下篇将会是后台定位相关的介绍.

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS推送小结

标签:ios   推送   

原文地址:http://blog.csdn.net/qq329735967/article/details/47440879

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!