极光推送(JPush)开发随笔
极光推送集成步骤:
1.登录IOS Dev Center创建推送证书

App id创建,使用Explicit App ID的方式创建并开启Push Notification功能
注意这里必须使用指定具体Bundle ID的方式不要使用通配符

描述文件Provisioning Profiles中 App ID需要修改成之前创建开启Push Notification的App ID.
2.下载配置和证书并从钥匙串中将推送证书以.P12的格式导出
3.上传证书,并将app的Bundle ID填写上去

4.将描述文件导入xcode
5.xcode开启Remote notifications

6.导入API开发包到应用程序项目
将SDK包解压,在Xcode中选择“Add files to '你的项目名称'”,将解压后的lib子文件夹里的APService.h 和libPushSDK.a添加到工程目录中
必要的框架
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Xcode7需要的是libz.tbd;Xcode7以下版本是libz.dylib
Build Settings
设置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为"$(SRCROOT)"即可
- (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
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
// Required
//如需兼容旧版本的方式,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化和同时使用pushConfig.plist文件声明appKey等配置内容。
[JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction]; //一般使用上面的方法,这个是新版本推出的方法。
可以在这里设置
// [JPUSHService setAlias:@"123" callbackSelector:nil object:nil];//对某一个特地用户手机推送的时候会用到。
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required
[JPUSHService registerDeviceToken:deviceToken];//获得APNS返回的设备标识符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);
// 应用正处理前台状态下,不会收到推送消息,因此在此处需要额外处理一下
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收到推送消息"
message:userInfo[@"aps"][@"alert"]
delegate:nil
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil];
[alert show];
}
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];//这个方法可以写在任意你想写的地方,数量代表桌面图标上面推送通知的条数。一般写在程序启动之后。
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
注册接收自定义消息的通知
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSLog(@"收到了自定义信息");
NSDictionary * userInfo = [notification userInfo];
NSString * content = [userInfo valueForKey:@"content"];
NSDictionary *extras = [userInfo valueForKey:@"extras"];
NSLog(@"extras = %@",extras);
NSLog(@"content = %@",content);
//建立本地通知,如果程序在后台的时候也会收到推送通知一样的消息。也可以判断在程序在前台的时候做一些特别的操作。
[JPUSHService setLocalNotification:[NSDate dateWithTimeIntervalSinceNow:10] alertBody:@"收到了自定义信息" badge:1 alertAction:@"adada" identifierKey:nil userInfo:nil soundName:nil];
}
7.监听通知
建议开发者加上API里面提供下面 5 种类型的通知:
extern NSString * const kJPFNetworkDidSetupNotification; // 建立连接
extern NSString * const kJPFNetworkDidCloseNotification; // 关闭连接
extern NSString * const kJPFNetworkDidRegisterNotification; // 注册成功
extern NSString * const kJPFNetworkDidLoginNotification; // 登录成功
温馨提示:
Registration id 需要在执行到kJPFNetworkDidLoginNotification的方法里获取
extern NSString * const kJPFNetworkDidReceiveMessageNotification; // 收到自定义消息(非APNS)
其中,kJPFNetworkDidReceiveMessageNotification传递的数据可以通过NSNotification中的userInfo方法获取,包括标题、内容、extras信息等
iOS SDK 集成指南
http://docs.jiguang.cn/guideline/ios_guide/#sdk