1.极光推送包括:APNs推送,JPush应用内消息。
JPush 代理开发者的应用(需要基于开发者提供的应用证书),向苹果 APNs 服务器推送。由 APNs Server 推送到 iOS 设备上。
JPush 应用内推送部分,即 App 启动时,内嵌的 JPush SDK 会开启长连接到 JPush Server,从而 JPush Server 可以推送消息到 App 里。
2.根据极光文档中JPush集成指南配置证书,导入框架,配置工程。
3.在appDelegate的didfinishlaunching方法里添加
//注册极光推送
[JPUSHService setupWithOption:nil appKey:@"" channel:nil apsForProduction:YES];
//获取自定义消息
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
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];
}
#pragma mark 获取自定义消息内容
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
NSString *content = [userInfo valueForKey:@"content"];
NSDictionary *extras = [userInfo valueForKey:@"extras"];
NSString *customizeField1 = [extras valueForKey:@"123456"]; //自定义参数,key是自己定义的
NSLog(@"自定义message:%@",userInfo);
NSLog(@"推%@",content);
NSLog(@"推%@",extras);
NSLog(@"推%@",customizeField1);
}
#pragma mark 接收推送的消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"推送的消息:NSDictionary:%@",userInfo);
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark 极光推送添加的内容
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[JPUSHService registerDeviceToken:deviceToken];
}
#pragma mark 失败回调方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
#pragma mark 极光推送设置别名和标签,后台可以根据别名和签名给指定的用户发送消息
温馨提示:设置别名和签名时注意call back的处理结果,返回值为0才设置成功
if ([dic[@"weProtectUserType"] isEqualToString:@"administrator"]) {
NSMutableSet *set = [[NSMutableSet alloc] initWithObjects:@"A", nil];
[JPUSHService setTags:set alias:alisa fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
NSLog(@"设置签名与别名1:%d",iResCode);
NSLog(@"推送返回的%@%@",iTags,iAlias);
} ];
}else
{
NSMutableSet *set = [[NSMutableSet alloc] initWithObjects:@"C", nil];
[JPUSHService setTags:set alias:alisa fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
NSLog(@"设置签名与别名2:%d",iResCode);
NSLog(@"推送返回的%@%@",iTags,iAlias);
} ];
}