本文匹配的 SDK版本:r1.2.5 之後。ios
查看最近更新瞭解最新的SDK更新狀況。api
極光推送(JPush)是一個端到端的推送服務,使得服務器端消息可以及時地推送到終端用戶手機上,讓開發者積極地保持與用戶的鏈接,從而提升用戶活躍度、提升應用的留存率。極光推送客戶端支持 Android, iOS 兩個平臺。服務器
本 iOS SDK 方便開發者基於 JPush 來快捷地爲 iOS App 增長推送功能,減小集成 APNs 須要的工做量、開發複雜度。app
包名爲JPush-iOS-SDK-[版本號]框架
* 建立成功後自動生成 AppKey 用以標識該應用。 fetch
在你的工程中建立一個新的Property List文件,並將其命名爲PushConfig.plist,填入Portal爲你的應用提供的APP_KEY等參數。ui
{ "APS_FOR_PRODUCTION" = "0"; "CHANNEL" = "Publish channel"; "APP_KEY" = "AppKey copied from JPush Portal application"; }
APIs 主要集中在 APService 接口類裏。spa
@interface APService : NSObject // init Push + (void)setupWithOption:(NSDictionary *)launchingOption; // register notification type + (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types; // upload device token + (void)registerDeviceToken:(NSData *)deviceToken; // handle notification recieved + (void)handleRemoteNotification:(NSDictionary *)remoteInfo;
監聽系統事件,相應地調用 JPush SDK 提供的 API 來實現功能。代理
如下 3 個事件監聽與調用 JPush SDK API 都是必須的。請直接複製以下代碼塊裏,註釋爲 "Required" 的行,到你的應用程序代理類裏相應的監聽方法裏。code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; // Required if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { //能夠添加自定義categories [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else { //categories 必須爲nil [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; } // Required [APService setupWithOption:launchOptions]; return YES; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Required [APService registerDeviceToken:deviceToken]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Required [APService handleRemoteNotification:userInfo]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // IOS 7 Support Required [APService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); }
API裏面提供了下面 5 種類型的通知:
extern NSString * const kJPFNetworkDidSetupNotification; // 創建鏈接
extern NSString * const kJPFNetworkDidCloseNotification; // 關閉鏈接
extern NSString * const kJPFNetworkDidRegisterNotification; // 註冊成功
extern NSString * const kJPFNetworkDidLoginNotification; // 登陸成功
extern NSString * const kJPFNetworkDidReceiveMessageNotification; // 收到消息(非APNS)
其中,kJPFNetworkDidReceiveMessageNotification通知是有傳遞數據的,能夠經過NSNotification中的userInfo方法獲取,包括標題、內容、內容類型、擴展信息等