iOS極光推送

            昨天花了一下午的時間研究了下極光推送,也前也是沒作過,不知道從何下手!纔開始的時候一看官方的SDK感受好難,不過通過一系列的搗鼓以後,手機收到了推送信息,感受其實並無那麼難!ios

 

 

1.配置開發證書(得有開發者帳號,我的,企業的均可以)

 

 

 

 

開發環境測試

 

在對 JPush iOS 開發環境進行測試前,請確保 3 個統一:api

  • App 是開發環境打包(開發證書 Development)
  • 上傳了開發證書並驗證經過
  • Portal 上的應用設置爲開發環境

發佈環境測試

在對 JPush iOS 生產環境進行測試前,請確保 3 個統一:xcode

  • App 是 ad-hoc 打包或者App Store 版本(發佈證書 Production)
  • 上傳了發佈證書並驗證經過
  • Portal 上的應用設置爲生產環境

iOS 證書 設置指南

建立應用程序ID

jpush_ios

  • 建立 App ID,若是 ID 已經存在能夠直接跳過此步驟

jpush_ios

  • 爲 App 開啓 Push Notification 功能。若是是已經建立的 App ID 也能夠經過設置開啓 Push Notification 功能。

jpush_ios

根據實際狀況完善 App ID 信息並提交,注意此處須要指定具體的 Bundle ID 不要使用通配符。app

jpush_ios

配置和下載證書

  • 若是你以前沒有建立過 Push 證書或者是要從新建立一個新的,請在證書列表下面新建。

  • 新建證書須要注意選擇證書種類(開發證書用於開發和調試使用,生產證書用於 App Store 發佈)

  • 點擊 Continue 後選擇證書對應的應用ID,而後繼續會出現「About Creating a Certificate Signing Request (CSR)」。

  • 根據它的說明建立打開KeychainAccess 建立 Certificate Signing Request。

  • 填寫「User Email Address」和「Common Name」 後選擇 Saved to disk 進行保存 。

  • 繼續返回Apple developer 網站點擊 Continue ,上傳剛剛生成的 .certSigningRequest 文件生成 APNs Push Certificate。
  • 下載並雙擊打開證書,證書打開時會啓動「鑰匙串訪問」工具。
  • 在「鑰匙串訪問」中你的證書會顯示在「個人證書」中,注意選擇「My Certificates」 和"login"

jpush_ios

導出 .p12 證書文件

注意要選「login」和「My Certificates」 導出證書時要選中證書文件,不要展開private key。

jpush_ios

  • 將文件保存爲Personal Information Exchange (.p12)格式。

  • 將文件保存爲Personal Information Exchange (.p12)格式。

上傳證書

在 JPush 管理 Portal 上,針對某應用程序,上傳上面步驟獲得 .p12 證書文件。這是 iOS SDK 可以接收到 JPush 推送消息的必要步驟。框架

Provisioning Profile的建立

  • 建立Provisioning Profile的前提,已在Apple Developer網站建立待發布應用所使用的Bundle ID的App ID,且爲該App ID建立了APNs證書,以下圖:

jpush_ios

jpush_ios

  • 選擇此Provisioning Profile的環境後點擊[Continue]:

jpush_ios

  • 選擇要建立Provisioning Profile的App ID後點擊[Continue]:

jpush_ios

  • 選擇所屬的開發者證書,(這裏建立了多個開發者證書,建議只建立一個,方便管理)爲了方便,選擇了[Select All],再點擊[Continue]進入下一步:

jpush_ios

  • 爲該Provisioning Profile選擇將要安裝的設備(通常選擇[Select All]),點擊[Continue]:

jpush_ios

  • 給該Provisioning Profile填寫Profile Name,點擊[generate]完成建立。

jpush_ios

  • 填寫完Profile Name後點擊[generate]完成建立,以後點擊[DownLoad]下載Provisioning Profile

jpush_ios

  • 雙擊下載下來的Provisioning Profile,添加到xcode。
 
 
 

 

2. Xcode的證書配置教程

參照iOS SDK 集成指南集成JPush SDK 和上傳了推送用到的p12證書後在編譯運行前須要先配置一下證書,步驟以下:工具

  • 打開xxx-info.plist的Bundle identifier項把上傳到JPush 控制檯的bundle id填寫進去:

jpush_ios

  • 點擊項目,選擇目標TARGETS後進入Build Setting 界面,搜索「Code signing」,按照下圖配置

jpush_ios

 

 

客戶端設置

開啓Remote notifications

 

須要在Xcode 中修改應用的 Capabilities 開啓Remote notifications,請參考下圖:測試



 

 

3.SDK集成步驟

 

 

 

集成壓縮包內容

 

包名爲JPush-iOS-SDK-{版本號}fetch

  • lib文件夾:包含頭文件 JPUSHService.h,靜態庫文件jpush-ios-x.x.x.a ,支持的iOS版本爲 5.0 及以上版本。(請注意:模擬器不支持APNs)
  • pdf文件:集成指南
  • demo文件夾:示例

開發環境

  • 使用Xcode 6及以上版本可使用新版Push SDK,XCode 5環境下須要運行舊版本SDK(1.7.4)

 

一、在JPush Portal上建立應用

  • 在 JPush的管理Portal 上建立應用並上傳APNs證書。若是對APNs證書不太瞭解 請參考: iOS 證書設置指南

jpush_ios

  • 建立成功後自動生成 AppKey 用以標識該應用。

jpush_ios

二、導入API開發包到應用程序項目

  • 將SDK包解壓,在XCode中選擇「Add files to 'Your project name'...」,將解壓後的lib子文件夾(包含JPUSHService.h、jpush-ios-x.x.x.a)添加到你的工程目錄中。

三、必要的框架

  • CFNetwork.framework
  • CoreFoundation.framework
  • CoreTelephony.framework
  • SystemConfiguration.framework
  • CoreGraphics.framework
  • Foundation.framework
  • UIKit.framework
  • Security.framework
  • Xcode7須要的是libz.tbd;Xcode7如下版本是libz.dylib

四、Build Settings

若是你的工程須要支持小於7.0的iOS系統,請到Build Settings 關閉 bitCode 選項,不然將沒法正常編譯經過。

  • 設置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,好比SDK文件夾(默認爲lib)與工程文件在同一級目錄下,則都設置爲"$(SRCROOT)/{靜態庫所在文件夾名稱}"便可。

五、建立並配置PushConfig.plist文件

 

2.1.0 版本開始,新增了帶參數的setupWithOption初始化方法,可經過此方法等參數傳入AppKey等信息。1.8.8及以前版本的 JPush SDK只能經過PushConfig.plist配置AppKey等信息。

 

 

在你的工程中建立一個新的Property List文件,並將其命名爲PushConfig.plist,文件所含字段以下:

  • CHANNEL
    • 指明應用程序包的下載渠道,爲方便分渠道統計,具體值由你自行定義,如:App Store。
  • APP_KEY
    • 填寫管理Portal上建立應用後自動生成的AppKey值。請確保應用內配置的 AppKey 與第1步在 Portal 上建立應用後生成的 AppKey 一致。
  • APS_FOR_PRODUCTION
    • 1.3.1版本新增,用於標識當前應用所使用的APNs證書環境。
    • 0 (默認值)表示採用的是開發證書,1 表示採用生產證書發佈應用。
    • 注:此字段的值要與Build Settings的Code Signing配置的證書環境一致。
  • 在1.2.2或以前版本的配置文件中,有 TEST_MODE 這個鍵,新版的SDK再也不使用,能夠將它刪除。

PushConfig.plist文件示例圖:

jpush_ios

六、添加代碼

 

2.1.0版本開始,API類名爲JPUSHService,再也不使用原先的APService。

 

容許XCode7支持Http傳輸方法

若是用的是Xcode7時,須要在App項目的plist手動加入如下key和值以支持http傳輸:

<key>NSAppTransportSecurity</key> 
      <dict> 
  <key>NSAllowsArbitraryLoads</key> 
        <true/> 
    </dict>

集成所需API

APIs 主要集中在 JPUSHService 接口類裏。

  • 初始化JPush方法分爲兩個:

    • 1.8.8及如下版本使用的是已過時的初始化方法。升級到2.1.0的老用戶仍可繼續使用舊的初始化方法。
    • 2.1.0版本開始提供帶appkey等參數的新初始化方法。使用此方法無需再添加PushConfig.plist配置JPush的AppKey等字段。
 

4.項目代碼:

 

 AppDelegate.h裏面的代碼:

 

#import<UIKit/UIKit.h>

staticNSString *appKey = @"4fd48a0712a3fde75eb1c7423";//申請應用成功之後官方會提供給你

staticNSString *channel = @"Publish channel";

staticBOOL isProduction = FALSE;

 

@interface AppDelegate :UIResponder <UIApplicationDelegate>

 

@property (strong,nonatomicUIWindow *window;

 

@end

 

 

 

#import"AppDelegate.m"中的代碼

 

 

#import "AppDelegate.h"

#import "JPUSHService.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 8.0) {

        //能夠添加自定義categories

        [JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge |

                                                         UIUserNotificationTypeSound |

                                                         UIUserNotificationTypeAlert)

                                              categories:nil];

    } else {

        //categories 必須爲nil

        [JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

                                                         UIRemoteNotificationTypeSound |

                                                         UIRemoteNotificationTypeAlert)

                                              categories:nil];

        

    }

    [JPUSHServicesetupWithOption:launchOptions appKey:appKey

                          channel:channelapsForProduction:NO];

    returnYES;

}

 

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

    

    // Required

    [JPUSHServiceregisterDeviceToken:deviceToken];

}

 

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

    

    // Required,For systems with less than or equal to iOS6

    [JPUSHServicehandleRemoteNotification:userInfo];

}

 

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

    

    // IOS 7 Support Required

    [JPUSHServicehandleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);

}

 

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    

    //Optional

    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

}

 

 

 

 

 

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

 

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

 

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

 

- (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.

}

 

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

 

@end

 

 

 

4.真機測試:

 
登錄到極光推送官網:
 
 
 
點擊控制檯:
 

 

選擇應用,也就是你建立測試的項目:個人項目是aa.TestPush

 

選中之後點擊進入到下面這裏:

 

點擊上面的推送:

 

 

點擊左邊的發送通知,也能夠選擇自定義消息(這裏我選擇的發送通知):

 

這裏我隨便寫點什麼用我手機測試:

 

 

 

點擊當即發送:

 

 

 

 

 

手機上的顯示:

相關文章
相關標籤/搜索