極光推送教程

創(chuàng)建應用
在 JPush的管理Portal 上創(chuàng)建應用并上傳APNs證書。如果對APNs證書不太了解 請參考: iOS 證書設置指南

jpush_ios

創(chuàng)建成功后自動生成 AppKey 用以標識該應用。

jpush_ios

配置工程
導入SDK
將SDK包解壓,在Xcode中選擇“Add files to 'Your project name'...”,將解壓后的lib子文件夾(包含JPUSHService.h、jpush-ios-x.x.x.a,jcore-ios-x.x.x.a)添加到你的工程目錄中。
添加

FrameworkCFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
libz.tbd (Xcode7以下版本是libz.dylib)
AdSupport.framework (獲取IDFA需要;如果不使用IDFA,請不要添加)
UserNotifications.framework (Xcode8及以上)
libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)

Build Settings
如果你的工程需要支持小于7.0的iOS系統(tǒng),請到Build Settings 關閉 bitCode 選項,否則將無法正常編譯通過。
設置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夾(默認為lib)與工程文件在同一級目錄下,則都設置為"$(SRCROOT)/{靜態(tài)庫所在文件夾名稱}"即可。

Capabilities
如使用Xcode8及以上環(huán)境開發(fā),請開啟Application Target的Capabilities->Push Notifications選項,如圖:

jpush_ios

允許Xcode7支持Http傳輸方法
如果您使用的是2.1.9及以上的版本則不需要配置此步驟如果用的是Xcode7或更新版本,需要在App項目的plist手動配置下key和值以支持http傳輸:
選擇1:根據(jù)域名配置
在項目的info.plist中添加一個Key:NSAppTransportSecurity,類型為字典類型。
然后給它添加一個NSExceptionDomains,類型為字典類型;
把需要的支持的域添加給NSExceptionDomains。其中jpush.cn作為Key,類型為字典類型。
每個域下面需要設置2個屬性:NSIncludesSubdomains、NSExceptionAllowsInsecureHTTPLoads。 兩個屬性均為Boolean類型,值分別為YES、YES。

如圖:

jpush_ios

選擇2:全局配置

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

添加頭文件
請將以下代碼添加到 AppDelegate.m 引用頭文件的位置。

// 引入JPush功能所需頭文件
#import "JPUSHService.h"
// iOS10注冊APNs所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的頭文件(可選)
#import <AdSupport/AdSupport.h>

添加Delegate
為AppDelegate添加Delegate。
參考代碼:

@interface AppDelegate ()<JPUSHRegisterDelegate>
@end

添加初始化代碼
2.1.0版本開始,API類名為JPUSHService,不再使用原先的APService。

添加初始化APNs代碼
請將以下代碼添加到

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//Required 
//notice: 3.0.0及以后版本注冊可以這樣寫,也可以繼續(xù)用之前的注冊方式 
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; 
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound; 
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
   // 可以添加自定義categories 
   // NSSet<UNNotificationCategory *> *categories for iOS10 or later 
   // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9 
} 
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

添加初始化JPush代碼
請將以下代碼添加到-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

// Optional 
// 獲取IDFA 
// 如需使用IDFA功能請?zhí)砑哟舜a并在初始化方法的advertisingIdentifier參數(shù)中填寫對應值
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
 // Required 
// init Push 
// notice: 2.1.5版本的SDK新增的注冊方法,改成可上報IDFA,如果沒有使用IDFA直接傳nil 
// 如需繼續(xù)使用pushConfig.plist文件聲明appKey等配置內(nèi)容,請依舊使用[JPUSHService setupWithOption:launchOptions]方式初始化。 
[JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];

部分參數(shù)說明:
appKey 填寫管理Portal上創(chuàng)建應用后自動生成的AppKey值。請確保應用內(nèi)配置的 AppKey 與 Portal 上創(chuàng)建應用后生成的 AppKey 一致。

channel 指明應用程序包的下載渠道,為方便分渠道統(tǒng)計,具體值由你自行定義,如:App Store。

apsForProduction1.3.1版本新增,用于標識當前應用所使用的APNs證書環(huán)境。
0 (默認值)表示采用的是開發(fā)證書,1 表示采用生產(chǎn)證書發(fā)布應用。
注:此字段的值要與Build Settings的Code Signing配置的證書環(huán)境一致。

advertisingIdentifier詳見關于IDFA。

注冊APNs成功并上報DeviceToken
請在AppDelegate.m實現(xiàn)該回調(diào)方法并添加回調(diào)方法中的代碼

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
/// Required - 注冊 DeviceToken
 [JPUSHService registerDeviceToken:deviceToken];
}

實現(xiàn)注冊APNs失敗接口(可選)

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

添加處理APNs通知回調(diào)方法
請在AppDelegate.m實現(xiàn)該回調(diào)方法并添加回調(diào)方法中的代碼

#pragma mark- JPUSHRegisterDelegate

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  // Required
  NSDictionary * userInfo = notification.request.content.userInfo;
  if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
  }
  completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設置
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  // Required
  NSDictionary * userInfo = response.notification.request.content.userInfo;
  if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
  }
  completionHandler();  // 系統(tǒng)要求執(zhí)行這個方法
}

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

  // Required, iOS 7 Support
  [JPUSHService handleRemoteNotification:userInfo];
  completionHandler(UIBackgroundFetchResultNewData);
}

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

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

添加處理JPush自定義消息回調(diào)方法
如需使用JPush的自定義消息功能,請參考文檔來實現(xiàn)自定義消息的處理回調(diào)方法。
成功運行
真機調(diào)試該項目,如果控制臺輸出以下日志則代表您已經(jīng)集成成功。

2016-08-19 17:12:12.745823 219b28[1443:286814]  | JPUSH | I - [JPUSHLogin] 
----- login result -----  
uid:5460310207   
registrationID:171976fa8a8620a14a4  

如果調(diào)試運行中遇到問題請參考:iOS SDK 調(diào)試指南
高級功能
關于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;

JPush SDK 相關事件監(jiān)聽
建議開發(fā)者加上API里面提供的以下類型的通知:
extern NSString *const kJPFNetworkIsConnectingNotification; // 正在連接中
extern NSString * const kJPFNetworkDidSetupNotification; // 建立連接
extern NSString * const kJPFNetworkDidCloseNotification; // 關閉連接
extern NSString * const kJPFNetworkDidRegisterNotification; // 注冊成功
extern NSString *const kJPFNetworkFailedRegisterNotification; //注冊失敗
extern NSString * const kJPFNetworkDidLoginNotification; // 登錄成功

溫馨提示: 
Registration id 需要添加注冊kJPFNetworkDidLoginNotification通知的方法里獲取,
也可以調(diào)用[registrationIDCompletionHandler:]方法,通過completionHandler獲取

extern NSString * const kJPFNetworkDidReceiveMessageNotification; // 收到自定義消息(非APNs)
其中,kJPFNetworkDidReceiveMessageNotification傳遞的數(shù)據(jù)可以通過NSNotification中的userInfo方法獲取,包括標題、內(nèi)容、extras信息等
請參考文檔:iOS SDK API

下面是推送證書相關設置
也就是最上面第一個圖里提到的推送證書,這里我先放的是開發(fā)環(huán)境下的推送證書

Paste_Image.png
Paste_Image.png

看看是否push notifications 開啟,如果未開啟,點擊下面的Edit進行編輯

Paste_Image.png

這里需要的推送證書需要提前準備好,然后在這里選擇證書


Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png

然后去鑰匙串里生成

Paste_Image.png
Paste_Image.png
Paste_Image.png

![Uploading Paste_Image_128597.png . . .]


Paste_Image.png

點擊下載備用

Paste_Image.png

然后在剛才app id的配置里選擇這個推送證書就可以了

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容