iOS學習之極光推送,ios極光推

來源:互聯網
上載者:User

iOS學習之極光推送,ios極光推
一、極光推送工程端

1、下載SDK

 極光推送是一個推送訊息的第三方,SDK下載:https://www.jpush.cn/common/products

 整合壓縮包內容:包名為JPush-iOS-SDK-{版本號碼}

  • lib檔案夾:包含標頭檔 JPUSHService.h,靜態庫檔案jpush-ios-x.x.x.a ,支援的iOS版本為 5.0 及以上版本。(請注意:模擬器不支援APNs)

  • pdf檔案:整合指南

  • demo檔案夾:樣本

 真如上面的介紹,我們在工程中需要 lib 檔案夾,因此就只需要將 lib 檔案夾 匯入。

2、匯入必要的架構

  • 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,請不要添加)

3、plist 檔案建立之坑

 在極光的協助文檔中有一個坑,就是 建立並配置PushConfig.plist檔案 。如果初始化 JPUSHService 使用極光 <= 1.8.8版本的SDK的註冊方法(+ (void)setupWithOption:(NSDictionary *)launchingOption; 以棄用)就建立,如果使用之後版本的初始化方法,就不需要。我在下面就使用現在最新的一個版本 2.1.5,所以就沒有建立 PushConfig.plist檔案

4、代碼

 現在 AppDelegate 中匯入如下的兩個標頭檔

// 極光服務標頭檔#import "JPUSHService.h"// ASIdentifierManager控制器標頭檔,廣告,可以不使用#import <AdSupport/ASIdentifierManager.h>

 調用代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.        // 廣告(一般不使用)//    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:@"App Store" // 指明應用程式套件組合的下載渠道,為方便分渠道統計,具體值由你自行定義                 apsForProduction:0 // 1.3.1版本新增,用於標識當前應用所使用的APNs認證環境 0 (預設值)表示採用的是開發認證,1 表示採用生產認證發布應用            advertisingIdentifier:nil]; // 不使用就將advertisingIdentifier置為nil//            advertisingIdentifier:advertisingId];        // 當你入坑建立了plist檔案,也可以使用這個初始化方法//    [JPUSHService setupWithOption:launchOptions];        return YES;}#pragma mark - 監聽- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {        /// Required - 註冊 DeviceToken    NSLog(@"%d == deviceToken == %@",__LINE__, deviceToken);    [JPUSHService registerDeviceToken:deviceToken];}// 反饋給伺服器- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {        // Required,For systems with less than or equal to iOS6        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;        [JPUSHService handleRemoteNotification:userInfo];}- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {    //Optional    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);}

5、關於廣告(IDFA)

 r2.1.5版本增加一個上傳IDFA字串的介面:

+ (void)setupWithOption:(NSDictionary *)launchingOption                  appKey:(NSString *)appKey                 channel:(NSString *)channel        apsForProduction:(BOOL)isProduction   advertisingIdentifier:(NSString *)advertisingId; 

 這個方法也就是我在上面使用的,如果不使用IDFA,仍可使用下面的介面:

+ (void)setupWithOption:(NSDictionary *)launchingOption                  appKey:(NSString *)appKey                 channel:(NSString *)channel        apsForProduction:(BOOL)isProduction;

 總結:三種初始化方法可以根據你自己的需求隨意選擇。

6、允許XCode7支援Http傳輸方法

 如果用的是Xcode7時,需要在App項目的plist手動設定下key和值以支援http傳輸:

 選擇1:根據網域名稱配置

  • 在項目的info.plist中添加一個Key:App Transport Security Settings,類型為字典類型。

  • 字典中添加一個Key:Allow Arbitrary Loads,類型為Boolean類型,值為YES。

  

 選擇2:全域配置

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

以上就是你在工程中所有的配置,下面就是關於極光推送伺服器端的操作!

二、極光推送伺服器端

1、註冊使用者

2、建立應用

3、發送通知

 通知內容直接填寫就可以

 推送對象需要就行一下選擇,如:

 RegistrationID會在你工程的控制台列印,你可以根據這個來給一個人進行發送通知

以上是本人對極光推送的一個小總結和整理,大家有更好的話,就連結!!!!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.