項目實戰:iOS極光推送整合(30分鐘搞定)

來源:互聯網
上載者:User

標籤:tde   systemv   cancel   ber   開發環境   dma   gdi   float   article   

推送有非常多,如個推、友盟、融雲和極光等等。在這裡就講下怎樣使用極光推送。

主要內容是將官方文檔資料詳細匯總並一步一步整合到項目中,您也能夠直接去官方文檔閱覽。

極光推送SDK下載

直接開啟官方文檔下載最新的SDK

極光網建立APP並上傳認證

一、製作推送認證和描寫敘述檔案:須要注意要製作兩個認證,一個是測試認證,一個是公布認證,詳細看官方文檔:點擊查看認證製作
直接看《iOS 認證 設定指南》其它的不用看了,廢話太多

二、在極光推送官網注冊你的APP吧。

bundle id要和你項目一直,假設是測試就選擇開發環境。上線後再切換下就能夠了;開發環境和生產環境的認證千萬別弄混了。是p12檔案,還有password的。

項目配置問題

1、直接將SDK中的lib目錄拖到項目中,裡面主要是兩個檔案,.a靜態安裝包和.h檔案。還有Demo中一個音效檔要加入。
2、加入必要的架構

CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Adsupport.framework (擷取IDFA須要)

Xcode7須要的是libz.tbd;Xcode7下面版本號碼是libz.dylib

3、假設SDK是2.1.0後的,直接編譯,假設報錯,會有一個bitcode錯誤,直接到Build Settings 關閉 bitCode 選項;
假設還有報錯,看下是否是由於找不到.a的靜態庫。把靜態庫地址拖過去就能夠了。


4、假設是老版本號碼的,SDK1.8.8建立並配置PushConfig.plist檔案,

假設是最新的SDK,直接跳過了。直接忽視這條就能夠了

5、假設用的是Xcode7時,須要在App項目的plist手動加入下面key和值以支援http傳輸:

1.在Info.plist中加入NSAppTransportSecurity類型Dictionary。
2.在NSAppTransportSecurity下加入NSAllowsArbitraryLoads類型Boolean,值設為YES
注意:不是單元測試的info.plist

6、開啟推送設定

7、開啟xxx-info.plist的Bundle identifier項把上傳到JPush 控制台的bundle id填寫進去:

項目代碼

你也能夠直接在第一步的SDK中查看詳細的Demo。
在Appdelegate.h匯入標頭檔

#import <AdSupport/AdSupport.h>#import "JPUSHService.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |                                                          UIUserNotificationTypeSound |                                                          UIUserNotificationTypeAlert)                                              categories:nil];    } else {        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |                                                          UIRemoteNotificationTypeSound |                                                          UIRemoteNotificationTypeAlert)                                              categories:nil];    }    [JPUSHService setupWithOption:launchOptions appKey:JPushAPPKEY                          channel:channel                 apsForProduction:isProduction            advertisingIdentifier:advertisingId];}

setupWithOption 方法中須要的參數

appkey:注冊的APPkey,網站上找去吧
channel:隨便寫個字串吧。也沒事
advertisingIdentifier:廣告標識符,(蘋果都廢棄了,這廝還要,哎)
apsForProduction:BOOL值,區分是開發環境還是測試環境

接下來是幾個回呼函數設定下就能夠了

- (void)applicationDidEnterBackground:(UIApplication *)application {   [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];}- (void)applicationWillEnterForeground:(UIApplication *)application {[application setApplicationIconBadgeNumber:0];[application cancelAllLocalNotifications]; }- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{    [JPUSHService registerDeviceToken:deviceToken];    NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);}- (void)application:(UIApplication *)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError *)error {    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{    NSLog(@"8.0收到通知:%@",[self logDic:userInfo]);    [JPUSHService handleRemoteNotification:userInfo];    completionHandler(UIBackgroundFetchResultNewData);}

詳細API參考詳細版本號碼SDK。我的SDK是2.1.5,最新的哦!
completionHandlez作用主要是2個:
1、並依據傳遞的UIBackgroundFetchResult 參數記錄新資料是否可用
2、調用完畢的處理代碼時。應用的介面縮圖會自己主動更新

測試推送

好了,你如今在真機跑下項目,然後就能夠去極光網站測試你的推送是否成功了(app在後台或者鎖屏狀態下都能夠收到通知的)

問題匯總

假設控制台沒有列印出你的訊息,說明你有問題了。

不要急。看下官方的問題匯總吧,反正你依照上面做的,保證沒問題的哦,》》》》》》問題匯總

項目實戰:iOS極光推送整合(30分鐘搞定)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.