http://blog.csdn.net/apple_app/article/details/39228221

极光推送 action设置 http://docs.jpush.cn/display/dev/IOS+8+UIUserNotificationSettings

一直更新了iOS8,但是一直没有开始研究这个iOS8,今天因为项目用到了推送,于是体验了iOS8的推送,先讲讲这个推送。目前分为四个推送:用户推送,本地推送,远程推送,地理位置推送。

 

用户推送

我们先开始讲这个用户推送,我们要使用之前必须先注册这个推送,用户要允许这个程序进行推送

注册过程:

  1. if (IS_IOS8) {
  2. //1.创建消息上面要添加的动作(按钮的形式显示出来)
  3. UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
  4. action.identifier = @"action";//按钮的标示
  5. action.title=@"Accept";//按钮的标题
  6. action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
  7. //    action.authenticationRequired = YES;
  8. //    action.destructive = YES;
  9. UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
  10. action2.identifier = @"action2";
  11. action2.title=@"Reject";
  12. action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
  13. action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
  14. action.destructive = YES;
  15. //2.创建动作(按钮)的类别集合
  16. UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
  17. categorys.identifier = @"alert";//这组动作的唯一标示,推送通知的时候也是根据这个来区分
  18. [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
  19. //3.创建UIUserNotificationSettings,并设置消息的显示类类型
  20. UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];
  21. [application registerUserNotificationSettings:notiSettings];
  22. }else{
  23. [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
  24. }
  1. - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
  2. {
  3. //    UIUserNotificationSettings *settings = [application currentUserNotificationSettings];
  4. //    UIUserNotificationType types = [settings types];
  5. //    //只有5跟7的时候包含了 UIUserNotificationTypeBadge
  6. //    if (types == 5 || types == 7) {
  7. //        application.applicationIconBadgeNumber = 0;
  8. //    }
  9. //注册远程通知
  10. [application registerForRemoteNotifications];
  11. }

我们现在仅仅是注册了通知的设置,还要注册推送通知的行为,在iOS8中,行为能直接在推送消息进行,如回复消息,拒绝消息等总结就是三个方法进行注册

 

我们如何能进行这些行为,首先我们需注册这些行为。

    • Actions

      1. UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
      2. acceptAction.identifier = @"RickAction";
      3. acceptAction.title = @"Accept";
      4. acceptAction.activationMode = UIUserNotificationActivationModeBackground;
      5. acceptAction.destructive = NO;
      6. acceptAction.authenticationRequired = NO;
    • Categories
      1. UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
      2. inviteCategory.identifier = @"INVITE_CATEGORY";
      3. [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];
       

      我们需要注意这个UIUserNotificationActionContextDefault,如果我们使用这个,我们会得到这个推送行为,Maybe和Accept

      我们还可以使用UIUserNotificationActionContextMinimal得到的是Decline和Accept行为

       
    • Settings

      在这些行为注册之后,我们加上之前提到的推送设置就完成了注册推送的这个流程了

      1. NSSet *categories = [NSSet setWithObjects:inviteCategory, nil nil];
      2. UIUserNotificationType  types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert ;
      3. UIUserNotificationSettings  *mySettings  = [UIUserNotificationSettings settingsForTypes:types categories:categories];
      4. [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

      远程推送

      远程推送,所有消息大小不超过2KB,我们获取远程推送的json格式的消息,解析这个消息就是我们的远程推送了:

      1. {
      2. “aps”: {
      3. "content-available": 1,
      4. "alert": "This is the alert text",
      5. "badge": 1,
      6. "sound": "default"
      7. }
      8. }

      若要使用远程推送,满足两个条件:一、用户需要调用注册用户推送registerUserNotificationSettings;二、在info.plist文件中UIBackgroundModes必须包含远程通知。

      1. <span style="font-family: Helvetica, Arial, Geneva, sans-serif;">[[UIApplication sharedApplication] registerForRemoteNotifications];</span>
      1. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
      2. NSString *token=[NSString stringWithFormat:@"%@",deviceToken];
      3. token=[token stringByReplacingOccurrencesOfString:@"<" withString:@""];
      4. token=[token stringByReplacingOccurrencesOfString:@">" withString:@""];
      5. token=[token stringByReplacingOccurrencesOfString:@" " withString:@""];
      6. }
      1. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
      2. }

      iOS7通知代理方法

       

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

       

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

       
      1. - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
      2. {
      3. }
      4. - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
      5. {
      6. }
      7. - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
      8. {
      9. if ([identifier isEqualToString:@"RickAction"]) {
      10. [self handleAcceptActionWithNotification:notification];
      11. }
      12. completionHandler();
      13. }
      14. - (void)handleAcceptActionWithNotification:(UILocalNotification*)notification
      15. {
      16. }

      地理位置推送

      这个推送是新的API才有的特性,必须配合CLLocation定位一起使用。

      1. //Location Notification
      2. CLLocationManager *locMan = [[CLLocationManager alloc] init];
      3. locMan.delegate = self;
      4. [locMan requestWhenInUseAuthorization];
      5. #pragma mark - CLLocationManager
      6. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
      7. {
      8. BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse);
      9. if (canUseLocationNotifications) {
      10. [self startShowLocationNotification];
      11. }
      12. }
      13. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
      14. {
      15. CLRegion *region = notification.region;
      16. if (region) {
      17. }
      18. }
      19. - (void)startShowLocationNotification
      20. {
      21. CLLocationCoordinate2D local2D ;
      22. local2D.latitude = 123.0;
      23. local2D.longitude = 223.0;
      24. UILocalNotification *locNotification = [[UILocalNotification alloc] init];
      25. locNotification.alertBody = @"你接收到了";
      26. locNotification.regionTriggersOnce = YES;
      27. locNotification.region = [[CLCircularRegion alloc] initWithCenter:local2D radius:45 identifier:@"local-identity"];
      28. [[UIApplication sharedApplication] scheduleLocalNotification:locNotification];
      29. }

      如果没有开启Core Location 那么上面的didReceiveLocalNotification不会被调用

      最后再总结一下,整个推送流程我觉得是这样子的,先注册推送,然后推送消息,客户端接收推送消息,执行推送行为。如果有错误,还请在文章下面评论,欢迎指正。

       
       

      这个博客写的也不错:http://blog.csdn.net/yujianxiang666/article/details/35260135

iOS iOS8注册通知的更多相关文章

  1. ios注册通知NSNotificationCenter(一)

    作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的. 注册通知:即要在什么地方接受消息 [[NSNotificationCenter defaultCenter]  ...

  2. iOS开发系列--通知与消息机制

    概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地 ...

  3. iOS开发系列--通知与消息机制--转

    来自:http://www.cocoachina.com/ios/20150318/11364.html 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户 ...

  4. “iOS 推送通知”详解:从创建到设置到运行

    这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...

  5. iOS pop使用通知传值

    iOS pop回父级页面,使用通知传值 输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IBAction)buttonClick:(id)sender { //添加 字 ...

  6. iOS开发之通知中心(NSNotificationCenter)

    前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...

  7. iOS Notification – 远程通知

    本文讲解iOS的远程通知的基本使用,主要包括远程通知的类型,处理远程通知的场景,以及远程通知相关证书的配置等等. 一.APNs简介 APNs是苹果公司提供的远程通知的服务器,当App处于后台或者没有运 ...

  8. iOS学习——(转)iOS中关于通知的使用

    在移动端开打过程中,经常会用到通知和推送,例如有短信来了需要通知提示,手机横屏了需要通知提示,插上耳机了需要通知提示等等,我们可以根据这些通知采取对应的动作.iOS系统自身定义了很对通知,但是在开发过 ...

  9. iOS开发-消息通知机制(NSNotification和NSNotificationCenter)

    iOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改变要映射到页 ...

随机推荐

  1. 【python之路34】面向对象作业之学生选课系统

    一.需求: 1.可以注册管理员账号,管理员账号可以创建老师和课程 2.学生可以注册和登陆,学生可以从课程列表选课,可以进行上课登记查看 二.代码 1.文件目录 bin 存放可执行文件 config 存 ...

  2. LUOGU P2441 角色属性树

    题目描述 绪萌同人社是一个有趣的组织,该组织结构是一个树形结构.有一个社长,直接下属一些副社长.每个副社长又直接下属一些部长--. 每个成员都有一个萌点的属性,萌点属性是由一些质数的萌元素乘积构成(例 ...

  3. PHP搜索优化 sphinx 搭建测试

    安装.环境:win7 64位 1.下载sphinx文件包 下载地址:http://sphinxsearch.com/downloads/archive/ 2.解压到D:/sphinx.新建文件夹dat ...

  4. JS高级---学习roadmap---5 parts

    JS高级---学习roadmap---5 parts part 1-3 part 4-5

  5. JDBC 操作数据库实例

    JDBC是什么 JDBC代表Java数据库连接(Java Database Connectivity),它是用于Java编程语言和数据库之间的数据库无关连接的标准Java API,换句话说:JDBC是 ...

  6. jQuery控制导航条样式

    原理:点击当前元素时,当前元素添加(样式类),父辈的兄弟姐妹的孩子('a')去掉此样式类. 代码如下: /*次要导航*/ $(".subnav li a").click(funct ...

  7. IO流9 --- 使用FileInputStream和FileOutputStream读写非文本文件 --- 技术搬运工(尚硅谷)

    字节流读写非文本文件(图片.视频等) @Test public void test5(){ File srcFile = new File("FLAMING MOUNTAIN.JPG&quo ...

  8. ROWID的使用——快速删除重复的记录

    ROWID是数据的详细地址,通过rowid,oracle可以快速的定位某行具体的数据的位置.ROWID可以分为物理rowid和逻辑rowid两种.普通的表中的rowid是物理rowid,索引组织表(I ...

  9. Oracle时间一串数字转为日期格式

    一.前台处理 js中接收到后台返回的json字符串中的日期类型的字段都变成了一串数字,例如:1500341149000.所以我们需要将这个串格式化形如:2017-07-18 09:25:49. 1.首 ...

  10. Nginx 编译设置模块执行顺序

    Nginx编译时,配置"--add-module=xxx"可以加入模块,当我们需要按照指定顺序来设置过滤模块执行顺序时,先配置的"--add-module=xxx&quo ...