昨天花了一下午的时间研究了下极光推送,也前也是没做过,不知道从何下手!才开始的时候一看官方的SDK感觉好难,不过经过一系列的捣鼓之后,手机收到了推送信息,感觉其实并没有那么难!
在对 JPush iOS 开发环境进行测试前,请确保 3 个统一:
在对 JPush iOS 生产环境进行测试前,请确保 3 个统一:
根据实际情况完善 App ID 信息并提交,注意此处需要指定具体的 Bundle ID 不要使用通配符。
注意要选“login”和“My Certificates” 导出证书时要选中证书文件,不要展开private key。
在 JPush 管理 Portal 上,针对某应用程序,上传上面步骤得到 .p12 证书文件。这是 iOS SDK 能够接收到 JPush 推送消息的必要步骤。
创建App ID、APN证书和p12证书的导出的具体步骤请看 :iOS 证书 设置指南
在苹果开发者账号的Provisioning Profile页面点击下图按钮,创建Provisioning Profile
参照iOS SDK 集成指南集成JPush SDK 和上传了推送用到的p12证书后在编译运行前需要先配置一下证书,步骤如下:
需要在Xcode 中修改应用的 Capabilities 开启Remote notifications,请参考下图:
包名为JPush-iOS-SDK-{版本号}
如果你的工程需要支持小于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传输:
NSAppTransportSecurity
NSAllowsArbitraryLoads
APIs 主要集中在 JPUSHService 接口类里。
初始化JPush方法分为两个:
AppDelegate.h里面的代码:
#import
static NSString *appKey = @"4fd48a0712a3fde75eb1c7423";//申请应用成功以后官方会提供给你
static NSString *channel = @"Publish channel";
static BOOL 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 = [UIColor whiteColor];
[self.window makeKeyAndVisible];
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel apsForProduction:NO];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required
[JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required,For systems with less than or equal to iOS6
[JPUSHService handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
[JPUSHService handleRemoteNotification: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
选中以后点击进入到下面这里:
点击上面的推送:
点击左边的发送通知,也可以选择自定义消息(这里我选择的发送通知):
这里我随便写点什么用我手机测试:
点击立即发送:
手机上的显示: