Xcode8 iOS10 極光推送兼容配置


Xcode8  iOS10 極光推送兼容配置

參考資料: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中, 引入頭文件

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. // 極光推送  
  2. #import "JPUSHService.h"  
  3. #import <AdSupport/AdSupport.h>  
  4. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  5. #import <UserNotifications/UserNotifications.h> // 這里是iOS10需要用到的框架  
  6. #endif  

 

 

7. 設置注冊極光推送需要的一些參數

 

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. static NSString * const JPUSHAPPKEY = @"xxxxxxxxxxxxxxxxx"; // 極光appKey  
  2. static NSString * const channel = @"Publish channel"; // 固定的  
  3.   
  4. #ifdef DEBUG // 開發  
  5.   
  6. static BOOL const isProduction = FALSE; // 極光FALSE為開發環境  
  7.   
  8. #else // 生產  
  9.   
  10. static BOOL const isProduction = TRUE; // 極光TRUE為生產環境  
  11.   
  12. #endif  

8. 這里是AppDelegate.m中的代碼, 分了幾大塊, 全部粘到下面, 直接復制可用(只需要下面這些代碼就可以實現通知)

 

 

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. //  
  2. //  AppDelegate.m  
  3. //  iOS10_JPUSH  
  4. //  
  5. //  Created by 周昊 on 16/9/18.  
  6. //  Copyright © 2016年 周昊. All rights reserved.  
  7. //  
  8.   
  9. #import "AppDelegate.h"  
  10. // 極光推送  
  11. #import "JPUSHService.h"  
  12. #import <AdSupport/AdSupport.h>  
  13. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  14. #import <UserNotifications/UserNotifications.h> // 這里是iOS10需要用到的框架  
  15. #endif  
  16.   
  17. static NSString * const JPUSHAPPKEY = @"xxxxxxxxxxxxxxxxx"; // 極光appKey  
  18. static NSString * const channel = @"Publish channel"; // 固定的  
  19.   
  20. #ifdef DEBUG // 開發  
  21.   
  22. static BOOL const isProduction = FALSE; // 極光FALSE為開發環境  
  23.   
  24. #else // 生產  
  25.   
  26. static BOOL const isProduction = TRUE; // 極光TRUE為生產環境  
  27.   
  28. #endif  
  29.   
  30. @interface AppDelegate ()<JPUSHRegisterDelegate> // 最新版的sdk需要實現這個代理方法  
  31.   
  32. @end  
  33.   
  34. @implementation AppDelegate  
  35.   
  36.   
  37. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  38.       
  39.     // 注冊apns通知  
  40.     if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) // iOS10  
  41.     {  
  42. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  43.         JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];  
  44.         entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge | UNAuthorizationOptionSound;  
  45.         [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];  
  46. #endif  
  47.     }  
  48.     else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) // iOS8, iOS9  
  49.     {  
  50.         //可以添加自定義categories  
  51.         [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];  
  52.     }  
  53.     else // iOS7  
  54.     {  
  55.         //categories 必須為nil  
  56.         [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];  
  57.     }  
  58.     /* 
  59.      *  launchingOption 啟動參數. 
  60.      *  appKey 一個JPush 應用必須的,唯一的標識. 
  61.      *  channel 發布渠道. 可選. 
  62.      *  isProduction 是否生產環境. 如果為開發狀態,設置為 NO; 如果為生產狀態,應改為 YES. 
  63.      *  advertisingIdentifier 廣告標識符(IDFA) 如果不需要使用IDFA,傳nil. 
  64.      * 此接口必須在 App 啟動時調用, 否則 JPush SDK 將無法正常工作. 
  65.      */  
  66.     // 廣告標識符  
  67.     NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];  
  68.       
  69.     // 如不需要使用IDFA,advertisingIdentifier 可為nil  
  70.     // 注冊極光推送  
  71.     [JPUSHService setupWithOption:launchOptions appKey:JPUSHAPPKEY channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];  
  72.       
  73.     //2.1.9版本新增獲取registration id block接口。  
  74.     [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {  
  75.         if(resCode == 0)  
  76.         {  
  77.             // iOS10獲取registrationID放到這里了, 可以存到緩存里, 用來標識用戶單獨發送推送  
  78.             NSLog(@"registrationID獲取成功:%@",registrationID);  
  79.             [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"];  
  80.             [[NSUserDefaults standardUserDefaults] synchronize];  
  81.         }  
  82.         else  
  83.         {  
  84.             NSLog(@"registrationID獲取失敗,code:%d",resCode);  
  85.         }  
  86.     }];  
  87.       
  88.     return YES;  
  89. }  
  90.   
  91. // ---------------------------------------------------------------------------------  
  92. - (void)applicationWillResignActive:(UIApplication *)application {  
  93. }  
  94.   
  95.   
  96. - (void)applicationDidEnterBackground:(UIApplication *)application {  
  97.     [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];  
  98. }  
  99.   
  100.   
  101. - (void)applicationWillEnterForeground:(UIApplication *)application {  
  102.     [application setApplicationIconBadgeNumber:0];  
  103.     [application cancelAllLocalNotifications];  
  104. }  
  105.   
  106.   
  107. - (void)applicationDidBecomeActive:(UIApplication *)application {  
  108. }  
  109.   
  110.   
  111. - (void)applicationWillTerminate:(UIApplication *)application {  
  112. }  
  113.   
  114. // ---------------------------------------------------------------------------------  
  115. #pragma mark - 注冊推送回調獲取 DeviceToken  
  116. #pragma mark -- 成功  
  117. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  
  118. {  
  119.     // 注冊成功  
  120.     // 極光: Required - 注冊 DeviceToken  
  121.     [JPUSHService registerDeviceToken:deviceToken];  
  122. }  
  123.   
  124. #pragma mark -- 失敗  
  125. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error  
  126. {  
  127.     // 注冊失敗  
  128.     NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);  
  129. }  
  130.   
  131. // ---------------------------------------------------------------------------------  
  132.   
  133. // 這部分是官方demo里面給的, 也沒實現什么功能, 放着以備不時之需  
  134. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1  
  135. - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings  
  136. {  
  137.       
  138. }  
  139.   
  140. // Called when your app has been activated by the user selecting an action from  
  141. // a local notification.  
  142. // A nil action identifier indicates the default action.  
  143. // You should call the completion handler as soon as you've finished handling  
  144. // the action.  
  145. - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler  
  146. {  
  147.       
  148. }  
  149.   
  150. // Called when your app has been activated by the user selecting an action from  
  151. // a remote notification.  
  152. // A nil action identifier indicates the default action.  
  153. // You should call the completion handler as soon as you've finished handling  
  154. // the action.  
  155. - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler  
  156. {  
  157.       
  158. }  
  159. #endif  
  160.   
  161. // ---------------------------------------------------------------------------------  
  162. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification  
  163. {  
  164.     [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];  
  165. }  
  166.   
  167. // ---------------------------------------------------------------------------------  
  168.   
  169. #pragma mark - iOS7: 收到推送消息調用  
  170. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {  
  171.       
  172.     // iOS7之后調用這個  
  173.     [JPUSHService handleRemoteNotification:userInfo];  
  174.     NSLog(@"iOS7及以上系統,收到通知");  
  175.       
  176.     if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0 || application.applicationState > 0)  
  177.     {  
  178.         // 程序在前台或通過點擊推送進來的會彈這個alert  
  179.         NSString *message = [NSString stringWithFormat:@"iOS7-8-9收到的推送%@", [userInfo[@"aps"] objectForKey:@"alert"]];  
  180.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil];  
  181.         [alert show];  
  182.     }  
  183.     completionHandler(UIBackgroundFetchResultNewData);  
  184. }  
  185.   
  186. // ---------------------------------------------------------------------------------  
  187.   
  188. #pragma mark - iOS10: 收到推送消息調用(iOS10是通過Delegate實現的回調)  
  189. #pragma mark- JPUSHRegisterDelegate  
  190. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  191. // 當程序在前台時, 收到推送彈出的通知  
  192. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {  
  193.       
  194.     NSDictionary * userInfo = notification.request.content.userInfo;  
  195.       
  196.     if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])  
  197.     {  
  198.         [JPUSHService handleRemoteNotification:userInfo];  
  199.         NSString *message = [NSString stringWithFormat:@"will%@", [userInfo[@"aps"] objectForKey:@"alert"]];  
  200.         NSLog(@"iOS10程序在前台時收到的推送: %@", message);  
  201.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil];  
  202.         [alert show];  
  203.     }  
  204.   
  205.     completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設置  
  206. }  
  207.   
  208.   
  209. // 程序關閉后, 通過點擊推送彈出的通知  
  210. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {  
  211.       
  212.     NSDictionary * userInfo = response.notification.request.content.userInfo;  
  213.       
  214.     if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])  
  215.     {  
  216.         [JPUSHService handleRemoteNotification:userInfo];  
  217.         NSString *message = [NSString stringWithFormat:@"did%@", [userInfo[@"aps"] objectForKey:@"alert"]];  
  218.         NSLog(@"iOS10程序關閉后通過點擊推送進入程序彈出的通知: %@", message);  
  219.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil];  
  220.         [alert show];  
  221.     }  
  222.    
  223.     completionHandler();  // 系統要求執行這個方法  
  224. }  
  225. #endif  
  226.   
  227. @end  

注: 極光的AppKey要自己到極光的官網申請哦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM