天天看點

xcode8更新後極光推送适配iOS10的相關問題

1.首先你需要在項目裡打開推送的開關,如下圖

xcode8更新後極光推送适配iOS10的相關問題

iOS9不打開也能收到推送,但是iOS10必須要打開

2.去極光官網下載下傳最新版的SDK替換項目裡的舊版SDK,如下圖是最新的SDK

xcode8更新後極光推送适配iOS10的相關問題

3.在項目裡适配

在AppDelegate.m裡導入
           
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#import <AdSupport/AdSupport.h>
#endif
           
同時注冊協定
           
在方法裡注冊推送
           
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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

    //這裡判斷系統版本
    if ([[UIDevice currentDevice].systemVersion floatValue] >= ) {
#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] >= ) {
        //可以添加自定義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 == ){
            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的推送還有更多功能等待發現

繼續閱讀