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. js的Date()时间对象

    var nowDate = new Date(); nowDate.getYear(); //获取当前年份(2位) nowDate.getFullYear(); //获取完整的年份(4位,1970-? ...

  2. 基础篇-1.4Java流程语句的基础

    1 条件语句 条件语句,即类似 if...else... 的语句,一个if语句包含了一个布尔表达式,以及一个或多个语句. if语句语法 if(布尔表达式) { // 布尔表达式为true时执行的语句块 ...

  3. python基础--常用的模块(collections、time、datetime、random、os、sys、json、pickle)

    collection模块: namedtuple:它是一个函数,是用来创建一个自定义的tuple对象的,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素.所以我们就可以 ...

  4. oracle-表空间-用户-角色-权限

    概要图 概要图 一 表空间 1.1创建表空间 --问题:创建一个名为hp的表空间,指定数据文件为hp.dbf,大小为10m. create tablespace hp datafile 'C:\app ...

  5. java-静态-单例-继承

    概要图 一.静态 1.1 静态方法 创建对象就是为了产生实例,并进行数据的封装. 而调用功能时,确没有用到这些对象中封装的数据. 该对象的创建有意义吗?虽然可以编译并运行,但是在堆内存中空间较为浪费. ...

  6. 2019-9-2-windows-10「设置」应用完整ms-settings快捷方式汇总

    title author date CreateTime categories windows-10「设置」应用完整ms-settings快捷方式汇总 lindexi 2019-09-02 12:57 ...

  7. 如何制作可以在 MaxCompute 上使用的 crcmod

    之前我们介绍过在 PyODPS DataFrame 中使用三方包.对于二进制包而言,MaxCompute 要求使用包名包含 cp27-cp27m 的 Wheel 包.但对于部分长时间未更新的包,例如 ...

  8. IIS 配置问题

    1 IIS错误需要重新运行配置 重新注册.netframework. 解决方式:cmd   C:\Windows\Microsoft.NET\Framework\v4.0.30319 aspnet_r ...

  9. Excel怎么增加撤销操作的次数?Excel增加可撤销次数教程

    Excel怎么增加撤销操作的次数?Excel增加可撤销次数教程 在Excel的使用中,返回上一步是经常用到的一个工具,当数据填写有误需要查看之前的内容时,一般会通过"Ctrl Z" ...

  10. No.5 Verilog 建模方式

    5-1 门级建模 VerilogHDL内建基元门: 多输入门:and, nand, or, nor, xor, xnor; 多输出门:buf, not 三态门:bufif0, bufif1, noti ...