iOS iOS8注册通知

一直更新了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];

  }

 

 

 

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

 

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

 

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

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

 

 

地理位置推送

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

 

 

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

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

原文地址:https://www.cnblogs.com/ChouDanDan/p/5113351.html