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

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

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

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

这里写图片描述

3.在项目里适配

在AppDelegate.m里导入
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#import <AdSupport/AdSupport.h>
#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的推送还有更多功能等待发现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值