iOS 極光推送整合與開發

zhf_Zachariah發表於2017-12-13

在進行以下操作時,開發證書或者釋出證書要申請成功

Mac鑰匙串--》鑰匙串訪問--》證書助理--》從證書頒發機構請求證書--》生成CSR檔案
(證書密碼為極光推送上傳證書時的密碼)```
######登入[開發者](https://developer.apple.com/account)網站
複製程式碼

建立AppID--》(#Bundle ID和應用一致,測試推送功能記得勾選#Push Notifications) --》建立證書,此時需要CSR檔案 下載證書 --》雙擊證書,右鍵檢視簡介新增信任,在鑰匙串中匯出為.p12檔案 在極光註冊應用時上傳開發證書或者釋出證書 --》完成註冊 #描述檔案有問題的可以重新生成描述檔案(主要作用是給應用和AppID、證書建立關聯)

####1.匯入靜態庫
複製程式碼

下載的極光推送SDK包解壓後,將其中的Lib檔案拖入工程中,然後匯入相關的靜態庫

下載SDK,將需要的兩個檔案匯入專案中:
![jpushsdk.png](http://upload-images.jianshu.io/upload_images/1398091-12278e5fe027de26.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
```整合壓縮包內容
包名為JPush-iOS-SDK-{版本號}
lib資料夾:包含標頭檔案 JPUSHService.h,靜態庫檔案jpush-ios-x.x.x.a ,支援的iOS版本為 5.0 及以上版本。
//(請注意:模擬器不支援APNs)
pdf檔案:整合指南
demo資料夾:示例```
![靜態庫.png](http://upload-images.jianshu.io/upload_images/1398091-ec2a7be673e7b73a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
####2.程式碼配置
複製程式碼

//在AppDelegate的相關方法中進行程式碼配置 #import "JPUSHService.h" #import <AdSupport/AdSupport.h>

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; //Required 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]; } //Required // 如需繼續使用pushConfig.plist檔案宣告appKey等配置內容,請依舊使用[JPUSHService setupWithOption:launchOptions]方式初始化。 [JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel//釋出平臺,nil apsForProduction:isProduction//BOOL 值 advertisingIdentifier:advertisingId ]; }

  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    /// Required - 註冊 DeviceToken [JPUSHService registerDeviceToken:deviceToken]; }

  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    // Required,For systems with less than or equal to iOS6 [JPUSHService handleRemoteNotification:userInfo]; }

  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    // IOS 7 Support Required [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); // 應用正處理前臺狀態下,不會收到推送訊息,因此在此處需要額外處理一下 if (application.applicationState == UIApplicationStateActive) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收到推送訊息" message:userInfo[@"aps"][@"alert"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil]; [alert show]; } }

  • (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); }

  • (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. [application setApplicationIconBadgeNumber:0]; return; }

####3.Xcode設定
複製程式碼

在info.plist檔案中設定允許http訪問 如果你的工程需要支援小於7.0的iOS系統,請到Build Settings 關閉 bitCode 選項,否則將無法正常編譯通過

![http.png](http://upload-images.jianshu.io/upload_images/1398091-cc865ecb4b955c74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
複製程式碼

設定允許推送

![Xcode.png](http://upload-images.jianshu.io/upload_images/1398091-4fd31e5a3d963070.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
####4.然後連結真機執行測試成功,Xcode會列印如下:
![列印結果.png](http://upload-images.jianshu.io/upload_images/1398091-000cfcde468a26dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
####5.在極光推送官網傳送測試資訊,測試時將應用在真機退出到後臺執行
![測試資訊.png](http://upload-images.jianshu.io/upload_images/1398091-de080b866100bc90.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

資料:
     [推送的自定義categories](http://my.oschina.net/u/1418722/blog/317422)
[iOS開發中的遠端推送實現(最新,支援iOS9)](https://zm10.sm-tc.cn/?src=http%3A%2F%2Fwww.mamicode.com%2Finfo-detail-1124741.html&uid=48a54dfb9031a7654198c539e9001d2f&hid=55b226f6266805a848e1c24be8b9cddd&pos=1&cid=9&time=1466556151388&from=click&restype=1&pagetype=0000004000000402&bu=structure_web_info&query=ios9)
[iOS: 極光推送](http://www.cnblogs.com/XYQ-208910/p/5463802.html)
複製程式碼

相關文章