製做證書的過程詳細看官方文檔或者以下推薦:html
http://jingyan.baidu.com/article/c1465413975cba0bfcfc4ccf.htmlios
http://docs.jpush.io/client/ios_tutorials/#ios_1app
http://docs.jpush.io/guideline/ios_guide/框架
http://allluckly.cn/投稿/tuogao28?utm_source=tuicool&utm_medium=referralless
建立完證書,就是去極光官網註冊帳號,建立應用,截圖以下:ide
將建立的證書上傳到應用上了,上傳成功後的截圖以下:工具
證書上傳成功後,生成APP Key,截圖以下:fetch
好了,這下工做作完了,剩下的就是代碼實現了:ui
第一步:下載SDK,將須要的兩個文件導入項目中:spa
包名爲JPush-iOS-SDK-{版本號}
第二步:導入須要依賴的庫文件:
第三步:建立一個工具類,名稱爲KJJPushHelper,封裝註冊時的各類方法
.h
// // KJJPushHelper.h // // Created by mac on 16/5/5. // Copyright © 2016年 mac. All rights reserved. // #import <Foundation/Foundation.h> @interface KJJPushHelper : NSObject // 在應用啓動的時候調用 + (void)setupWithOption:(NSDictionary *)launchingOption appKey:(NSString *)appKey channel:(NSString *)channel apsForProduction:(BOOL)isProduction advertisingIdentifier:(NSString *)advertisingId; // 在appdelegate註冊設備處調用 + (void)registerDeviceToken:(NSData *)deviceToken; // ios7之後,纔有completion,不然傳nil + (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion; // 顯示本地通知在最前面 + (void)showLocalNotificationAtFront:(UILocalNotification *)notification; @end
.m
// // KJJPushHelper.m // Created by mac on 16/5/5. // Copyright © 2016年 mac. All rights reserved. // #import "KJJPushHelper.h" #import "JPUSHService.h" @implementation KJJPushHelper + (void)setupWithOption:(NSDictionary *)launchingOption appKey:(NSString *)appKey channel:(NSString *)channel apsForProduction:(BOOL)isProduction advertisingIdentifier:(NSString *)advertisingId{ // Required #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1 // ios8以後能夠自定義category if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // 能夠添加自定義categories [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else { #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0 // ios8以前 categories 必須爲nil [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; #endif } #else // categories 必須爲nil [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; #endif // Required [JPUSHService setupWithOption:launchingOption appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId]; return; } + (void)registerDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:deviceToken]; return; } + (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion { [JPUSHService handleRemoteNotification:userInfo]; if (completion) { completion(UIBackgroundFetchResultNewData); } return; } + (void)showLocalNotificationAtFront:(UILocalNotification *)notification { [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil]; return; } @end
第四步:建立一個APPDelegate的分類,在該類中調用KJJPushHelper中的類方法
// AppDelegate+KJJPushSDK.h // // Created by mac on 16/5/5. // Copyright © 2016年 mac. All rights reserved. // #import "AppDelegate.h" @interface AppDelegate (KJJPushSDK) -(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; @end // AppDelegate+KJJPushSDK.m // // Created by mac on 16/5/5. // Copyright © 2016年 mac. All rights reserved. // #import "AppDelegate+KJJPushSDK.h" #import "KJJPushHelper.h" #define JPushSDK_AppKey @"31e01f6a2f6d4b1209061aec" #define isProduction NO @implementation AppDelegate (KJJPushSDK) -(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [KJJPushHelper setupWithOption:launchOptions appKey:JPushSDK_AppKey channel:nil apsForProduction:isProduction advertisingIdentifier:nil]; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Required - 註冊 DeviceToken [KJJPushHelper registerDeviceToken:deviceToken]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Required,For systems with less than or equal to iOS6 [KJJPushHelper handleRemoteNotification:userInfo completion:nil]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // IOS 7 Support Required [KJJPushHelper handleRemoteNotification:userInfo completion:completionHandler]; // 應用正處理前臺狀態下,不會收到推送消息,所以在此處須要額外處理一下 if (application.applicationState == UIApplicationStateActive) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收到推送消息" message:userInfo[@"aps"][@"alert"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"肯定",nil]; [alert show]; } } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [KJJPushHelper showLocalNotificationAtFront:notification]; return; } - (void)applicationDidBecomeActive:(UIApplication *)application { [application setApplicationIconBadgeNumber:0]; return; } @end
第五步:在AppDelegate中註冊便可
//註冊極光推送 [self JPushApplication:application didFinishLaunchingWithOptions:launchOptions];
好了,大功告成,插上真機運行:打印結果以下
若是出現下列錯誤信息JPUSH | W - [JPUSHClientController] Not get deviceToken yet. Maybe: your certificate not configured APNs? or current network is not so good so APNs registration failed? or there is no APNs register code? Please refer to JPush docs.
修改方法:
因爲項目以前用到了環信SDK,環信得已經註冊了通知,在AppDelegate中註冊通知,didRegisterForRemoteNotificationsWithDeviceToken與didFailToRegisterForRemoteNotificationsWithError方法,均不執行。。。需到環信註冊通知的地方,再次註冊極光通知。方能夠獲取到Token執行。