美文网首页
iOS 10 极光推送适配

iOS 10 极光推送适配

作者: 像风一样的孩子丶 | 来源:发表于2016-09-30 14:32 被阅读529次

前段时间升级xcode8.设备也升级了iOS 10.发现程序运行的时候,推送发生了错误.无法注册通知成功.因为我是用的是极光推送.于是下载了最新的JPush-SDK,进行了修改适配.那么下面就分享一下极光推送的iOS 10适配.

1.下载最新的JPush-SDK,替换掉之前的.
没设置SDK路径的也需要重新设置一下路径.(Target->Build Settings->Search Path->User Header Search Paths)

2.iOS 10的推送需要导入UserNotification.framework这个框架.

3.在Target->Capabilities中打开Push Notifications,打开了会生成一个xxxxxx.entitlements文件.(我暂时还没用到)

4.既然导入了新的框架,那就先导入头文件.

#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

这里我只在iOS版本超过iOS 9的最大版本的时候,才导入.因为还在适配低版本.

5.极光也更新了新的注册通知方法

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = (UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound);
         [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    }

然后最新的极光SDK提供了新的获取registrationID的block.

    //获取registration id
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0) {
            NSLog(@"registrationID:%@",registrationID);
        } else {
            NSLog(@"registrationID获取失败,code:%d",resCode);
        }
    }];

可以从上边获取方法,也可以从RemoteNotifications的注册协议中获取

//获取device token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [JPUSHService registerDeviceToken:deviceToken];
    NSString *jPushToken = [JPUSHService registrationID];
    NSLog(@"registrationID = %@", jPushToken);
}

6.启动极光SDK

[JPUSHService setupWithOption:launchOptions appKey:@"你的key" channel:@"App Store" apsForProduction:JPushIsProduction];

7.处理通知.
iOS10 以下的版本还是原来的方法处理.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    // IOS 7 Support Required
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    
    NSString *message = aps[@"alert"];
    NSLog(@"message = %@", message);
}

iOS 10的处理通知

#pragma mark - 处理推送消息  iOS 10
// iOS 10 Support 前台处理逻辑
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
//    completionHandler(UNNotificationPresentationOptionAlert); // 这个选择是否在前台进行提醒,声音,alert.还有角标.

    NSDictionary *aps = userInfo[@"aps"];
    NSString *message = aps[@"alert"];
    NSLog(@"message = %@", message);
}

// iOS 10 Support 后台处理逻辑
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系统要求执行这个方法

    NSDictionary *aps = userInfo[@"aps"];
    NSString *message = aps[@"alert"];
    NSLog(@"message = %@", message);
    }
}

配置完,就可以运行测试了.

相关文章

  • 【知识总结】(2)远程推送

    推送SDK:极光推送 后台点击推送: iOS 10 以下收到推送点击触发 iOS 10 以上触发: 极光推送中使用...

  • iOS 10 极光推送适配

    前段时间升级xcode8.设备也升级了iOS 10.发现程序运行的时候,推送发生了错误.无法注册通知成功.因为我是...

  • iOS 10极光推送适配(最新)

    iOS10发布后,发现项目中的极光推送接收消息异常了。查了相关资料后才发现,iOS10中对于通知做了不少改变。同时...

  • 极光推送适配iOS10

    1. 导入SDK 导入2.1.9版本的极光SDK 2. 导入SDK依赖的系统框架 CFNetwork.framew...

  • 技术贴合集

    iOS网络图片尺寸适配 iOS 10 消息推送(UserNotifications)秘籍总结 静默推送 iOS 面...

  • 个推我遇到的坑,

    iOS10,XCode8.1以来 推送据说各种变化 所以自然得适配。一拖再拖,今天和个推做个了断。之前用过极光 ...

  • iOS三方库资源汇总

    资源入口 iOS10的极光推送(感谢原作者)

  • iOS "_OBJC_CLASS_$_JPUSHSer

    项目中用了极光推送,想在模拟器看看IPhoneX 的适配情况报错。 iOS "OBJC_CLASS$_JPUSHS...

  • iOS-iOS10极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • iOS-极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

网友评论

      本文标题:iOS 10 极光推送适配

      本文链接:https://www.haomeiwen.com/subject/jpglyttx.html