iOS 10 极光推送适配

前段时间升级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);
    }
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS10发布后,发现项目中的极光推送接收消息异常了。查了相关资料后才发现,iOS10中对于通知做了不少改变。同时...
    深蓝_S阅读 779评论 0 0
  • 推送技术产生场景: --服务器端主动性: 客户端与服务器交互都是客户端主动的, 服务器一般不能主动与客户端进行数据...
    原军锋阅读 34,774评论 4 60
  • 不同版本极光推送SDK集成各有差异,集成时一定要注意版本号,楼主已将博文更新成最新的SDK JPush v3.0....
    i顺颂时宜阅读 7,896评论 37 170
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 6,764评论 10 16
  • 版本记录 前言   现在很多APP都有推送功能,其中极光推送就是很多APP的首选。我们最近的几个APP也是用的极光...
    刀客传奇阅读 8,485评论 0 8