iOS 10 极光推送适配

2016-09-30  本文已影响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);
    }
}

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

上一篇 下一篇

猜你喜欢

热点阅读