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

最新文章

  1. 如何利用python监控主机存活并邮件、短信通知
  2. REmap首次尝试--PC端
  3. PDO的一些操作
  4. apache 访问出现403 Forbidden
  5. ArchLinux 下架设PPTPD VPN服务
  6. 求1+2+3+...+n
  7. 如何在 Eclipse 中连接源码
  8. 【POJ】【2960】S-Nim
  9. UI进阶 动画
  10. [转+整理]LINUX学习笔记(1):磁盘结构及分区
  11. PHP学习之[第11讲]新浪微博开放平台 PHP 与 OAuth 接口(1)
  12. LayoutInflater和inflate()方法的使用方法
  13. windows环境下Mongodb分片配置
  14. service structure flowchart [mobile to server via HTTP RESTful API]
  15. 【第五篇】androidEventbus源代码阅读和分析之发送粘性事件和接收粘性事件代码分析
  16. 初始Django
  17. tree、find、tail命令重要实战
  18. BZOJ4825 单旋
  19. eclipse运行web项目注意有些坑
  20. Java调整JVM内存大小——(八)

热门文章

  1. [Array]1. Two Sum(map和unorder_map)
  2. LUOGU P3539 [POI2012]ROZ-Fibonacci Representation
  3. bzoj 2503 相框——思路
  4. bzoj 1800 [Ahoi2009]fly 飞行棋——模拟
  5. SSM3-SVN的安装和搭建环境
  6. Java中字符串为什么不以\0结尾
  7. maven的hibernate4 依赖
  8. js 正则去除html代码
  9. FZU 1576【计算几何/费马点】
  10. 重温 Webpack, Babel 和 React