xcode8升级后极光推送适配iOS10的相关问题

1.首先你需要在项目里打开推送的开关,如下图
这里写图片描述

iOS9不打开也能收到推送,但是iOS10必须要打开

2.去极光官网下载最新版的SDK替换项目里的旧版SDK,如下图是最新的SDK

这里写图片描述

3.在项目里适配

在AppDelegate.m里导入
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import 
#import 
#endif
同时注册协议
@interface AppDelegate ()<JPUSHRegisterDelegate>
在方法里注册推送
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

    //这里判断系统版本
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
    } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }


    // 读取PushConfig plist文件,载入极光推送相关信息
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PushConfig" ofType:@"plist"];
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];


    //如不需要使用IDFA,advertisingIdentifier 可为nil
    [JPUSHService setupWithOption:launchOptions appKey:dic[@"APP_KEY"]
                          channel:dic[@"CHANNEL"]
                 apsForProduction:[dic[@"APS_FOR_PRODUCTION"] boolValue]
            advertisingIdentifier:advertisingId];

    //2.1.9版本新增获取registration id block接口。
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID获取成功:%@",registrationID);

        }
        else{
            NSLog(@"registrationID获取失败,code:%d",resCode);
        }
    }];
}

4.最后在AppDelegate.m文件里写上极光提供的两个新方法并进行一系列的操作

#pragma mark- JPUSHRegisterDelegate
//前台收到通知调用此方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo]; }

    //这里是你要写的操作。比如弹出提示框,设置角标等
    /*
    [JPUSHService setBadge:[UIApplication sharedApplication].applicationIconBadgeNumber];
    //        application.applicationIconBadgeNumber = 0;
    [MGLoginTools managePush:userInfo];
    */
    completionHandler(UNNotificationPresentationOptionAlert); //                    Badge Sound Alert
}
//前台后台收到通知调用此方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo]; }

    //这里是你要写的操作。比如页面跳转之类的
    NSDictionary *apsDic = [userInfo objectForKey:@"aps"];
    NSString *strObj = [apsDic objectForKey:@"objectid"];
    NSString *titleName = [apsDic objectForKey:@"alert"];
    NSString *badge = [apsDic objectForKey:@"badge"];
    if (strObj == nil) {
        strObj = [userInfo objectForKey:@"objectid"];
    }
    NSDictionary *mesDic = @{@"objectid":strObj,
                             @"titleName":titleName,
                             @"badge":badge};
    //这里发通知进行页面跳转操作,可自定义
    [mNotificationCenter postNotificationName:kNotificationSystemPush object:nil userInfo:mesDic];
    completionHandler(); //
}

这样就更新成功了,iOS10的推送还有更多功能等待发现

你可能感兴趣的:(iOS开发)