iOS iOS8注册通知
http://blog.csdn.net/apple_app/article/details/39228221
极光推送 action设置 http://docs.jpush.cn/display/dev/IOS+8+UIUserNotificationSettings
一直更新了iOS8,但是一直没有开始研究这个iOS8,今天因为项目用到了推送,于是体验了iOS8的推送,先讲讲这个推送。目前分为四个推送:用户推送,本地推送,远程推送,地理位置推送。

用户推送
我们先开始讲这个用户推送,我们要使用之前必须先注册这个推送,用户要允许这个程序进行推送
注册过程:
- if (IS_IOS8) {
- //1.创建消息上面要添加的动作(按钮的形式显示出来)
- UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
- action.identifier = @"action";//按钮的标示
- action.title=@"Accept";//按钮的标题
- action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
- // action.authenticationRequired = YES;
- // action.destructive = YES;
- UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
- action2.identifier = @"action2";
- action2.title=@"Reject";
- action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
- action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
- action.destructive = YES;
- //2.创建动作(按钮)的类别集合
- UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
- categorys.identifier = @"alert";//这组动作的唯一标示,推送通知的时候也是根据这个来区分
- [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
- //3.创建UIUserNotificationSettings,并设置消息的显示类类型
- UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];
- [application registerUserNotificationSettings:notiSettings];
- }else{
- [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
- }
- - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- {
- // UIUserNotificationSettings *settings = [application currentUserNotificationSettings];
- // UIUserNotificationType types = [settings types];
- // //只有5跟7的时候包含了 UIUserNotificationTypeBadge
- // if (types == 5 || types == 7) {
- // application.applicationIconBadgeNumber = 0;
- // }
- //注册远程通知
- [application registerForRemoteNotifications];
- }
我们现在仅仅是注册了通知的设置,还要注册推送通知的行为,在iOS8中,行为能直接在推送消息进行,如回复消息,拒绝消息等总结就是三个方法进行注册

我们如何能进行这些行为,首先我们需注册这些行为。
- Actions
- UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
- acceptAction.identifier = @"RickAction";
- acceptAction.title = @"Accept";
- acceptAction.activationMode = UIUserNotificationActivationModeBackground;
- acceptAction.destructive = NO;
- acceptAction.authenticationRequired = NO;
- Categories
- UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
- inviteCategory.identifier = @"INVITE_CATEGORY";
- [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];

我们需要注意这个UIUserNotificationActionContextDefault,如果我们使用这个,我们会得到这个推送行为,Maybe和Accept
我们还可以使用UIUserNotificationActionContextMinimal得到的是Decline和Accept行为

- Settings
在这些行为注册之后,我们加上之前提到的推送设置就完成了注册推送的这个流程了
- NSSet *categories = [NSSet setWithObjects:inviteCategory, nil nil];
- UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert ;
- UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
- [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
远程推送
远程推送,所有消息大小不超过2KB,我们获取远程推送的json格式的消息,解析这个消息就是我们的远程推送了:
- {
- “aps”: {
- "content-available": 1,
- "alert": "This is the alert text",
- "badge": 1,
- "sound": "default"
- }
- }
若要使用远程推送,满足两个条件:一、用户需要调用注册用户推送registerUserNotificationSettings;二、在info.plist文件中UIBackgroundModes必须包含远程通知。
- <span style="font-family: Helvetica, Arial, Geneva, sans-serif;">[[UIApplication sharedApplication] registerForRemoteNotifications];</span>
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- NSString *token=[NSString stringWithFormat:@"%@",deviceToken];
- token=[token stringByReplacingOccurrencesOfString:@"<" withString:@""];
- token=[token stringByReplacingOccurrencesOfString:@">" withString:@""];
- token=[token stringByReplacingOccurrencesOfString:@" " withString:@""];
- }
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- }
iOS7通知代理方法

后来又增加了本地通知的代理方法

iOS8的推送代理方法只有两个了

- - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
- {
- }
- - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
- {
- }
- - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
- {
- if ([identifier isEqualToString:@"RickAction"]) {
- [self handleAcceptActionWithNotification:notification];
- }
- completionHandler();
- }
- - (void)handleAcceptActionWithNotification:(UILocalNotification*)notification
- {
- }
地理位置推送
这个推送是新的API才有的特性,必须配合CLLocation定位一起使用。
- //Location Notification
- CLLocationManager *locMan = [[CLLocationManager alloc] init];
- locMan.delegate = self;
- [locMan requestWhenInUseAuthorization];
- #pragma mark - CLLocationManager
- - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
- {
- BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse);
- if (canUseLocationNotifications) {
- [self startShowLocationNotification];
- }
- }
- - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
- {
- CLRegion *region = notification.region;
- if (region) {
- }
- }
- - (void)startShowLocationNotification
- {
- CLLocationCoordinate2D local2D ;
- local2D.latitude = 123.0;
- local2D.longitude = 223.0;
- UILocalNotification *locNotification = [[UILocalNotification alloc] init];
- locNotification.alertBody = @"你接收到了";
- locNotification.regionTriggersOnce = YES;
- locNotification.region = [[CLCircularRegion alloc] initWithCenter:local2D radius:45 identifier:@"local-identity"];
- [[UIApplication sharedApplication] scheduleLocalNotification:locNotification];
- }
如果没有开启Core Location 那么上面的didReceiveLocalNotification不会被调用
最后再总结一下,整个推送流程我觉得是这样子的,先注册推送,然后推送消息,客户端接收推送消息,执行推送行为。如果有错误,还请在文章下面评论,欢迎指正。

这个博客写的也不错:http://blog.csdn.net/yujianxiang666/article/details/35260135
iOS iOS8注册通知的更多相关文章
- ios注册通知NSNotificationCenter(一)
作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的. 注册通知:即要在什么地方接受消息 [[NSNotificationCenter defaultCenter] ...
- iOS开发系列--通知与消息机制
概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地 ...
- iOS开发系列--通知与消息机制--转
来自:http://www.cocoachina.com/ios/20150318/11364.html 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户 ...
- “iOS 推送通知”详解:从创建到设置到运行
这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...
- iOS pop使用通知传值
iOS pop回父级页面,使用通知传值 输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IBAction)buttonClick:(id)sender { //添加 字 ...
- iOS开发之通知中心(NSNotificationCenter)
前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...
- iOS Notification – 远程通知
本文讲解iOS的远程通知的基本使用,主要包括远程通知的类型,处理远程通知的场景,以及远程通知相关证书的配置等等. 一.APNs简介 APNs是苹果公司提供的远程通知的服务器,当App处于后台或者没有运 ...
- iOS学习——(转)iOS中关于通知的使用
在移动端开打过程中,经常会用到通知和推送,例如有短信来了需要通知提示,手机横屏了需要通知提示,插上耳机了需要通知提示等等,我们可以根据这些通知采取对应的动作.iOS系统自身定义了很对通知,但是在开发过 ...
- iOS开发-消息通知机制(NSNotification和NSNotificationCenter)
iOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改变要映射到页 ...
随机推荐
- js的Date()时间对象
var nowDate = new Date(); nowDate.getYear(); //获取当前年份(2位) nowDate.getFullYear(); //获取完整的年份(4位,1970-? ...
- 基础篇-1.4Java流程语句的基础
1 条件语句 条件语句,即类似 if...else... 的语句,一个if语句包含了一个布尔表达式,以及一个或多个语句. if语句语法 if(布尔表达式) { // 布尔表达式为true时执行的语句块 ...
- python基础--常用的模块(collections、time、datetime、random、os、sys、json、pickle)
collection模块: namedtuple:它是一个函数,是用来创建一个自定义的tuple对象的,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素.所以我们就可以 ...
- oracle-表空间-用户-角色-权限
概要图 概要图 一 表空间 1.1创建表空间 --问题:创建一个名为hp的表空间,指定数据文件为hp.dbf,大小为10m. create tablespace hp datafile 'C:\app ...
- java-静态-单例-继承
概要图 一.静态 1.1 静态方法 创建对象就是为了产生实例,并进行数据的封装. 而调用功能时,确没有用到这些对象中封装的数据. 该对象的创建有意义吗?虽然可以编译并运行,但是在堆内存中空间较为浪费. ...
- 2019-9-2-windows-10「设置」应用完整ms-settings快捷方式汇总
title author date CreateTime categories windows-10「设置」应用完整ms-settings快捷方式汇总 lindexi 2019-09-02 12:57 ...
- 如何制作可以在 MaxCompute 上使用的 crcmod
之前我们介绍过在 PyODPS DataFrame 中使用三方包.对于二进制包而言,MaxCompute 要求使用包名包含 cp27-cp27m 的 Wheel 包.但对于部分长时间未更新的包,例如 ...
- IIS 配置问题
1 IIS错误需要重新运行配置 重新注册.netframework. 解决方式:cmd C:\Windows\Microsoft.NET\Framework\v4.0.30319 aspnet_r ...
- Excel怎么增加撤销操作的次数?Excel增加可撤销次数教程
Excel怎么增加撤销操作的次数?Excel增加可撤销次数教程 在Excel的使用中,返回上一步是经常用到的一个工具,当数据填写有误需要查看之前的内容时,一般会通过"Ctrl Z" ...
- No.5 Verilog 建模方式
5-1 门级建模 VerilogHDL内建基元门: 多输入门:and, nand, or, nor, xor, xnor; 多输出门:buf, not 三态门:bufif0, bufif1, noti ...