昨天花了一下午的時間研究了下極光推送,也前也是沒作過,不知道從何下手!纔開始的時候一看官方的SDK感受好難,不過通過一系列的搗鼓以後,手機收到了推送信息,感受其實並無那麼難!ios
在對 JPush iOS 開發環境進行測試前,請確保 3 個統一:api
在對 JPush iOS 生產環境進行測試前,請確保 3 個統一:xcode
根據實際狀況完善 App ID 信息並提交,注意此處須要指定具體的 Bundle ID 不要使用通配符。app
注意要選「login」和「My Certificates」 導出證書時要選中證書文件,不要展開private key。
在 JPush 管理 Portal 上,針對某應用程序,上傳上面步驟獲得 .p12 證書文件。這是 iOS SDK 可以接收到 JPush 推送消息的必要步驟。框架
建立App ID、APN證書和p12證書的導出的具體步驟請看 :iOS 證書 設置指南less
在蘋果開發者帳號的Provisioning Profile頁面點擊下圖按鈕,建立Provisioning Profileide
參照iOS SDK 集成指南集成JPush SDK 和上傳了推送用到的p12證書後在編譯運行前須要先配置一下證書,步驟以下:工具
須要在Xcode 中修改應用的 Capabilities 開啓Remote notifications,請參考下圖:測試
包名爲JPush-iOS-SDK-{版本號}fetch
若是你的工程須要支持小於7.0的iOS系統,請到Build Settings 關閉 bitCode 選項,不然將沒法正常編譯經過。
2.1.0 版本開始,新增了帶參數的setupWithOption初始化方法,可經過此方法等參數傳入AppKey等信息。1.8.8及以前版本的 JPush SDK只能經過PushConfig.plist配置AppKey等信息。
在你的工程中建立一個新的Property List文件,並將其命名爲PushConfig.plist,文件所含字段以下:
PushConfig.plist文件示例圖:
2.1.0版本開始,API類名爲JPUSHService,再也不使用原先的APService。
若是用的是Xcode7時,須要在App項目的plist手動加入如下key和值以支持http傳輸:
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
APIs 主要集中在 JPUSHService 接口類裏。
初始化JPush方法分爲兩個:
AppDelegate.h裏面的代碼:
#import<UIKit/UIKit.h>
staticNSString *appKey = @"4fd48a0712a3fde75eb1c7423";//申請應用成功之後官方會提供給你
staticNSString *channel = @"Publish channel";
staticBOOL isProduction = FALSE;
@interface AppDelegate :UIResponder <UIApplicationDelegate>
@property (strong,nonatomic) UIWindow *window;
@end
#import"AppDelegate.m"中的代碼
#import "AppDelegate.h"
#import "JPUSHService.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 8.0) {
//能夠添加自定義categories
[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必須爲nil
[JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
[JPUSHServicesetupWithOption:launchOptions appKey:appKey
channel:channelapsForProduction:NO];
returnYES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required
[JPUSHServiceregisterDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required,For systems with less than or equal to iOS6
[JPUSHServicehandleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
[JPUSHServicehandleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
選擇應用,也就是你建立測試的項目:個人項目是aa.TestPush
選中之後點擊進入到下面這裏:
點擊上面的推送:
點擊左邊的發送通知,也能夠選擇自定義消息(這裏我選擇的發送通知):
這裏我隨便寫點什麼用我手機測試:
點擊當即發送:
手機上的顯示: