IOS8 推送第二篇

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

用户推送

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

注册过程:

if (IS_IOS8) {  
        //1.创建消息上面要添加的动作(按钮的形式显示出来)  
        UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];  
        action.identifier = @"action";//按钮的标示  
        [email protected]"Accept";//按钮的标题  
        action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序  
        //    action.authenticationRequired = YES;  
        //    action.destructive = YES;  
          
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  
        action2.identifier = @"action2";  
        [email protected]"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必须包含远程通知。

[[UIApplication sharedApplication] registerForRemoteNotifications];

- (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不会被调用

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

时间: 2024-08-29 10:55:52

IOS8 推送第二篇的相关文章

iOS8推送消息的快速回复处理

iOS8拥有了全新的通知中心,有全新的通知机制.当屏幕顶部收到推送时只需要往下拉,就能看到快速操作界面,并不需要进入该应用才能操作.在锁屏界面,对于推送项目也可以快速处理.基本上就是让用户尽量在不离开当前页面的前提下处理推送信息,再次提高处理效率. 能够进行直接互动的短信.邮件.日历.提醒,第三方应用,可以让你不用进入程序就能进行快捷操作,并专注于手中正在做的事情. 在通知横幅快速回复信息,不用进入短信程序: 可直接拒绝或接受邮件邀请: 可对提醒进行标记为完成或推迟: 当第三方应用更新接口后便可

个人微信公众号,每天推送一篇高质量文章

个人微信公众号,每天推送一篇高质量文章,喜欢的就订阅吧

Android推送 百度云推送 入门篇

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/27231237 现在app基本都有推送的功能,于是看了下百度云的推送,官方文档和Demo都很到位,记录下使用过程,目标是利用百度云推送最为服务器写个及时通讯的例子~当然了,这是第一篇入门~ 1.第一步就是在百度开发者服务管理中创建项目,然后拿到API key , Secret Key ;这个过程就不多说了,上官网直接申请就行,不复杂. 2.下载云推送的客户端SDK,SDK的压缩文

iOS8推送消息的回复处理速度

iOS8我们有一个新的通知中心,我们有一个新的通报机制.当在屏幕的顶部仅需要接收一个推拉向下,你可以看到高速接口,天赋并不需要输入应用程序的操作.锁定屏幕,用于高速处理可以推动项目. 推送信息,再次提高处理效率. 可以进行直接互动的短信.邮件.日历.提醒,第三方应用,可以让你不用进入程序就能进行快捷操作,并专注于手中正在做的事情. 在通知横幅高速回复信息,不用进入短信程序: 可直接拒绝或接受邮件邀请: 可对提醒进行标记为完毕或推迟: 当第三方应用更新接口后便可直接相应用进行高速操作.      

iOS7、iOS8推送通知的区别

iOS8版本以后的推送通知代码[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];//注意下面这句,在iOS8

【转】iOS8 推送 获取 devicetoken

标签:推送 push ios8 devicetoken token xcode6 原文:http://roybaby.blog.51cto.com/1508945/1557854 打开AppDelegate.m怎么做:首先: 1 2 3 4 5 6 7 8 9 10 11 12 13 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOption

iOS8 推送注册方式改变的问题

不久之后iPhone 6/6 plus就会在国内如雨后春笋般遍地开花了.iOS 8早已现行一步,不过有的开发者也注意到了在iOS 8上推送通知的注册方式有所变化,报错提示为: 1 registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later. 之后国外有开发者在使用了新的解决方案注册成功后发现在iPhone 6上仍然不能运行.鉴于iPhone 6/6 Plus将在国内开售,我们大可未雨绸缪一下. iOS 8因为

记录vue项目 用hbuilder离线打包集成极光推送 安卓篇

极光推送的官方demo: https://github.com/jpush/jpush-hbuilder-demo 里面也记录有详细的方法了. 我记录下自己的过程. 首先去极光那里创建一个应用 获取AppKey ,创建后如下. 然后把vue 项目npm run build 打包一下.用hbuilder 里面 发行-->本地打包--->生成本地包资源--->生成一个离线包. 然后去https://ask.dcloud.net.cn/article/103 里面下载HBUILDER最新的SD

ios8推送问题

registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsFor