前段時間升級xcode8.設備也升級了iOS 10.發(fā)現(xiàn)程序運行的時候,推送發(fā)生了錯誤.無法注冊通知成功.因為我是用的是極光推送.于是下載了最新的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的注冊協(xié)議中獲取
//獲取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(); // 系統(tǒng)要求執(zhí)行這個方法
NSDictionary *aps = userInfo[@"aps"];
NSString *message = aps[@"alert"];
NSLog(@"message = %@", message);
}
}
配置完,就可以運行測試了.