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的值改变要映射到页 ...
随机推荐
- 【python之路34】面向对象作业之学生选课系统
一.需求: 1.可以注册管理员账号,管理员账号可以创建老师和课程 2.学生可以注册和登陆,学生可以从课程列表选课,可以进行上课登记查看 二.代码 1.文件目录 bin 存放可执行文件 config 存 ...
- LUOGU P2441 角色属性树
题目描述 绪萌同人社是一个有趣的组织,该组织结构是一个树形结构.有一个社长,直接下属一些副社长.每个副社长又直接下属一些部长--. 每个成员都有一个萌点的属性,萌点属性是由一些质数的萌元素乘积构成(例 ...
- PHP搜索优化 sphinx 搭建测试
安装.环境:win7 64位 1.下载sphinx文件包 下载地址:http://sphinxsearch.com/downloads/archive/ 2.解压到D:/sphinx.新建文件夹dat ...
- JS高级---学习roadmap---5 parts
JS高级---学习roadmap---5 parts part 1-3 part 4-5
- JDBC 操作数据库实例
JDBC是什么 JDBC代表Java数据库连接(Java Database Connectivity),它是用于Java编程语言和数据库之间的数据库无关连接的标准Java API,换句话说:JDBC是 ...
- jQuery控制导航条样式
原理:点击当前元素时,当前元素添加(样式类),父辈的兄弟姐妹的孩子('a')去掉此样式类. 代码如下: /*次要导航*/ $(".subnav li a").click(funct ...
- IO流9 --- 使用FileInputStream和FileOutputStream读写非文本文件 --- 技术搬运工(尚硅谷)
字节流读写非文本文件(图片.视频等) @Test public void test5(){ File srcFile = new File("FLAMING MOUNTAIN.JPG&quo ...
- ROWID的使用——快速删除重复的记录
ROWID是数据的详细地址,通过rowid,oracle可以快速的定位某行具体的数据的位置.ROWID可以分为物理rowid和逻辑rowid两种.普通的表中的rowid是物理rowid,索引组织表(I ...
- Oracle时间一串数字转为日期格式
一.前台处理 js中接收到后台返回的json字符串中的日期类型的字段都变成了一串数字,例如:1500341149000.所以我们需要将这个串格式化形如:2017-07-18 09:25:49. 1.首 ...
- Nginx 编译设置模块执行顺序
Nginx编译时,配置"--add-module=xxx"可以加入模块,当我们需要按照指定顺序来设置过滤模块执行顺序时,先配置的"--add-module=xxx&quo ...