(吐槽,md的代码编辑功能不知道是不会用还是确实不好用)
请自行谷百.
//代码来源:环信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();
}
如果是程序正在运行或者说程序正在后台,那么这个时候处理推送消息的工作都是在:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
或者:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
中完成。
如果应用程序没有启动:
对application didFinishLaunchingWithOptions:的launchOptions参数说明如下:
每台iOS设备都会与APNs建立一个TLS连接,并凭此来接受推送消息.
如果设备处于断网离线状态,APNs会将要推送的消息缓存一小段时间,并在设备上线后推送消息.只能有一条时间最近的消息被缓存,如果在设备离线期间发送了多条消息,只会有时间上最后一条消息被推送.如果设备长期处于离线状态,那么所有缓存的离线消息都将被清空.
iOS8之后,推送消息的最大长度为2Kb,iOS8以前则为256bytes.
此外,还可以设置content-available字段来发送静默消息(用户界面不会有推送提示,将在之后发文介绍).
看了apple官方文档,发送推送的道道还挺多,预计将会是一系列的文章+示例进行说明.由于之后工作中会涉及到后台定位,所有下篇将会是后台定位相关的介绍.
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/qq329735967/article/details/47440879