參考資料:http://blog.csdn.net/cloud_pro/article/details/52574637
蘋果在iOS10上對apns推送做了修改, 極光也是很給力的, 在第一時間就對sdk進行了更新, 下面對iOS10注冊極光推送進行一下記錄.
首先, 在極光的開發者服務里注冊應用獲取appKey, 在apple Developer配置推送證書...等等等這些廢話就不說了.
兼容iOS10的是極光2.1.9版本的sdk.
1. 導入SDK
2. 導入SDK依賴的系統框架
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Xcode7需要的是libz.tbd ; Xcode7以下版本是libz.dylib
Adsupport.framework (獲取IDFA需要;如果不使用IDFA,請不要添加)
UserNotifications.framework(Xcode8及以上)
3. 設置Build Setting中, Search Paths的User Header Search Paths

4. 如果用的是Xcode8及以上環境開發需要開啟Application Target的Capabilities->Push Notifications選項
這兩個一定要都是對號 , 這個選項不開啟在iOS10后不會注冊成功
添加這個選項會在項目中多這樣一個文件
5. 不要忘記Xcode7以上需要支持http傳輸方式
下面是需要寫的代碼部分:
6. 在AppDelegate.m中, 引入頭文件
- // 極光推送
- #import "JPUSHService.h"
- #import <AdSupport/AdSupport.h>
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max
- #import <UserNotifications/UserNotifications.h> // 這里是iOS10需要用到的框架
- #endif
7. 設置注冊極光推送需要的一些參數
- static NSString * const JPUSHAPPKEY = @"xxxxxxxxxxxxxxxxx"; // 極光appKey
- static NSString * const channel = @"Publish channel"; // 固定的
- #ifdef DEBUG // 開發
- static BOOL const isProduction = FALSE; // 極光FALSE為開發環境
- #else // 生產
- static BOOL const isProduction = TRUE; // 極光TRUE為生產環境
- #endif
8. 這里是AppDelegate.m中的代碼, 分了幾大塊, 全部粘到下面, 直接復制可用(只需要下面這些代碼就可以實現通知)
- //
- // AppDelegate.m
- // iOS10_JPUSH
- //
- // Created by 周昊 on 16/9/18.
- // Copyright © 2016年 周昊. All rights reserved.
- //
- #import "AppDelegate.h"
- // 極光推送
- #import "JPUSHService.h"
- #import <AdSupport/AdSupport.h>
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max
- #import <UserNotifications/UserNotifications.h> // 這里是iOS10需要用到的框架
- #endif
- static NSString * const JPUSHAPPKEY = @"xxxxxxxxxxxxxxxxx"; // 極光appKey
- static NSString * const channel = @"Publish channel"; // 固定的
- #ifdef DEBUG // 開發
- static BOOL const isProduction = FALSE; // 極光FALSE為開發環境
- #else // 生產
- static BOOL const isProduction = TRUE; // 極光TRUE為生產環境
- #endif
- @interface AppDelegate ()<JPUSHRegisterDelegate> // 最新版的sdk需要實現這個代理方法
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // 注冊apns通知
- if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) // iOS10
- {
- #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) // iOS8, iOS9
- {
- //可以添加自定義categories
- [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
- }
- else // iOS7
- {
- //categories 必須為nil
- [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
- }
- /*
- * launchingOption 啟動參數.
- * appKey 一個JPush 應用必須的,唯一的標識.
- * channel 發布渠道. 可選.
- * isProduction 是否生產環境. 如果為開發狀態,設置為 NO; 如果為生產狀態,應改為 YES.
- * advertisingIdentifier 廣告標識符(IDFA) 如果不需要使用IDFA,傳nil.
- * 此接口必須在 App 啟動時調用, 否則 JPush SDK 將無法正常工作.
- */
- // 廣告標識符
- NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
- // 如不需要使用IDFA,advertisingIdentifier 可為nil
- // 注冊極光推送
- [JPUSHService setupWithOption:launchOptions appKey:JPUSHAPPKEY channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];
- //2.1.9版本新增獲取registration id block接口。
- [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
- if(resCode == 0)
- {
- // iOS10獲取registrationID放到這里了, 可以存到緩存里, 用來標識用戶單獨發送推送
- NSLog(@"registrationID獲取成功:%@",registrationID);
- [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- else
- {
- NSLog(@"registrationID獲取失敗,code:%d",resCode);
- }
- }];
- return YES;
- }
- // ---------------------------------------------------------------------------------
- - (void)applicationWillResignActive:(UIApplication *)application {
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application {
- [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application {
- [application setApplicationIconBadgeNumber:0];
- [application cancelAllLocalNotifications];
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application {
- }
- - (void)applicationWillTerminate:(UIApplication *)application {
- }
- // ---------------------------------------------------------------------------------
- #pragma mark - 注冊推送回調獲取 DeviceToken
- #pragma mark -- 成功
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
- // 注冊成功
- // 極光: Required - 注冊 DeviceToken
- [JPUSHService registerDeviceToken:deviceToken];
- }
- #pragma mark -- 失敗
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
- {
- // 注冊失敗
- NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
- }
- // ---------------------------------------------------------------------------------
- // 這部分是官方demo里面給的, 也沒實現什么功能, 放着以備不時之需
- #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
- - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- {
- }
- // Called when your app has been activated by the user selecting an action from
- // a local notification.
- // A nil action identifier indicates the default action.
- // You should call the completion handler as soon as you've finished handling
- // the action.
- - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
- {
- }
- // Called when your app has been activated by the user selecting an action from
- // a remote notification.
- // A nil action identifier indicates the default action.
- // You should call the completion handler as soon as you've finished handling
- // the action.
- - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
- {
- }
- #endif
- // ---------------------------------------------------------------------------------
- - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
- {
- [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
- }
- // ---------------------------------------------------------------------------------
- #pragma mark - iOS7: 收到推送消息調用
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
- // iOS7之后調用這個
- [JPUSHService handleRemoteNotification:userInfo];
- NSLog(@"iOS7及以上系統,收到通知");
- if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0 || application.applicationState > 0)
- {
- // 程序在前台或通過點擊推送進來的會彈這個alert
- NSString *message = [NSString stringWithFormat:@"iOS7-8-9收到的推送%@", [userInfo[@"aps"] objectForKey:@"alert"]];
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil];
- [alert show];
- }
- completionHandler(UIBackgroundFetchResultNewData);
- }
- // ---------------------------------------------------------------------------------
- #pragma mark - iOS10: 收到推送消息調用(iOS10是通過Delegate實現的回調)
- #pragma mark- JPUSHRegisterDelegate
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max
- // 當程序在前台時, 收到推送彈出的通知
- - (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];
- NSString *message = [NSString stringWithFormat:@"will%@", [userInfo[@"aps"] objectForKey:@"alert"]];
- NSLog(@"iOS10程序在前台時收到的推送: %@", message);
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil];
- [alert show];
- }
- completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設置
- }
- // 程序關閉后, 通過點擊推送彈出的通知
- - (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];
- NSString *message = [NSString stringWithFormat:@"did%@", [userInfo[@"aps"] objectForKey:@"alert"]];
- NSLog(@"iOS10程序關閉后通過點擊推送進入程序彈出的通知: %@", message);
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil];
- [alert show];
- }
- completionHandler(); // 系統要求執行這個方法
- }
- #endif
- @end
注: 極光的AppKey要自己到極光的官網申請哦