极光推送官网:https://www.jpush.cn
极光推送IOS官方文档:http://docs.jpush.cn/display/dev/iOS
集成方式
1、在JPush Portal上创建应用
在JPush的管理Portal上 上传证书并创建应用。如果对APNs证书不太了解 请参考 iOS 证书设置指南,
创建成功后自动生成 AppKey 用以标识该应用。
2、导入API开发包到应用程序项目
将SDK包解压,在XCode中选择“Add files to 'Your project name'...”,将解压后的lib子文件夹(包含APService.h、libPushSDK.a)添加到你的工程目录中。
3、必要的框架
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
libz.dylib
4、Build Settings
设置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为"$(SRCROOT)/[文件夹名称]"即可。
5、创建并配置PushConfig.plist文件
在你的工程中创建一个新的Property List文件,并将其命名为PushConfig.plist,填入Portal为你的应用提供的APP_KEY等参数。
{
"APS_FOR_PRODUCTION = "0";
"CHANNEL" = "Publish channel";
"APP_KEY" = "AppKey copied from JPush Portal application";
}
- CHANNEL
- APP_KEY
- APS_FOR_PRODUCTION
在1.2.2或之前版本的配置文件中,有 TEST_MODE 这个键,新版的SDK不再使用,可以将它删除。
6、添加代码:
在 APPDelegate.m文件中的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加代码:
//极光推送
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kAPNetworkDidSetupNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kAPNetworkDidCloseNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidRegister:) name:kAPNetworkDidRegisterNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kAPNetworkDidLoginNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kAPNetworkDidReceiveMessageNotification object:nil];
[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
// Required
[APService setupWithOption:launchOptions];
同时在APPDelegate.m 文件中添加代码
#pragma mark - 推送
- (void)networkDidSetup:(NSNotification *)notification {
NSLog(@"已连接");
}
- (void)networkDidClose:(NSNotification *)notification {
NSLog(@"未连接。。。");
}
- (void)networkDidRegister:(NSNotification *)notification {
NSLog(@"已注册");
}
- (void)networkDidLogin:(NSNotification *)notification {
NSLog(@"已登录");
NSLog(@"registrationID:%@",[APService registrationID]);
}
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
NSString *title = [userInfo valueForKey:@"title"];
NSString *content = [userInfo valueForKey:@"content"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSLog(@"%@",[NSString stringWithFormat:@"收到消息\ndate:%@\ntitle:%@\ncontent:%@", [dateFormatter stringFromDate:[NSDate date]],title,content]);
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[APService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[APService handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// 取得 APNs 标准信息内容
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量
NSString *sound = [aps valueForKey:@"sound"]; //播放的声音
NSDictionary *dic = @{@"Lo": [userInfo valueForKey:@"Lo"], @"La": [userInfo valueForKey:@"La"], @"address": [userInfo valueForKey:@"address"]};
[[SRViewController shareInstance] addPointAnnotationForAPNS:dic];
// 取得自定义字段内容
NSLog(@"content =[%@], badge=[%d], sound=[%@]",content,badge,sound);
// Required
// IOS 7 Support Required
[APService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
//设置badge
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[application setApplicationIconBadgeNumber:0];
}