iOS-極光推送集成與開發

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

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)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容