昨天花了一下午的时间研究了下极光推送,也前也是没作过,不知道从何下手!才开始的时候一看官方的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
选中之后点击进入到下面这里:
点击上面的推送:
点击左边的发送通知,也能够选择自定义消息(这里我选择的发送通知):
这里我随便写点什么用我手机测试:
点击当即发送:
手机上的显示: