极光官网设置
登录极光官网,完成注册、登录。
-
选择开发者服务->极光推送->立即体验
-
创建新的应用->应用名称(必填)、应用图标
-
应用设置->推送设置->选择iOS->上传需要的证书
-
验证证书
-
用设置->应用信息->复制我们需要的APPkey
-
发送通知设置-> 目标平台(开发环境、生产环境、Android、winphone)-> 目标人群(搜有人、标签、别名、ID、分组)->发送时间(立即、定时)
Demo设置
主要使用cocoapods方法集成极光,我这里使用了固定版本
pod 'JPush', ' ~> 3.0.9'
- 这里主要是写了一个AppDelegate的扩展
"AppDelegate+Push.h"
导入我们需要的头文件
import "AppDelegate+Push.h"
import <AdSupport/AdSupport.h>
import "GSBaseSonicWebViewController.h"
import <UserNotifications/UserNotifications.h>
主要介绍以下几个参数
launchingOption 启动参数.
appKey 一个JPush 应用必须的,唯一的标识.
channel 发布渠道. 可选.
isProduction 是否生产环境. 如果为开发状态,设置为 NO; 如果为生产状态,应改为 YES.
advertisingIdentifier 广告标识符(IDFA) 如果不需要使用IDFA,传nil.
此接口必须在 App 启动时调用, 否则 JPush SDK 将无法正常工作.
+(void)setupWithOption:(NSDictionary *)launchingOption
appKey:(NSString *)appKey
channel:(NSString *)channel
apsForProduction:(BOOL)isProduction
advertisingIdentifier:(NSString *)advertisingId;
- (void)pushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
NSSet<UNNotificationCategory *> *categories;
entity.categories = categories;
}
else {
NSSet<UIUserNotificationCategory *> *categories;
entity.categories = categories;
}
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
BOOL isProduction = YES;
#if ((DEBUG) || (ADHOC))//如果是开发环境
isProduction = NO;
#endif
[JPUSHService setupWithOption:launchOptions
appKey:appKey
channel:chanel
apsForProduction:isProduction
advertisingIdentifier:advertisingId];
//2.1.9版本新增获取registration id block接口。
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if(resCode == 0){
NSLog(@"registrationID获取成功:%@",registrationID);
}
else{
NSLog(@"registrationID获取失败,code:%d",resCode);
}
}];
[self setPushTag];
// JPush 监听登录成功
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkDidLogin:)
name:kJPFNetworkDidLoginNotification
object:nil] ;
}
- 设置标签tag和别名alias
- (void)setPushTag {
// 标签
__autoreleasing NSMutableSet *tags = [NSMutableSet set];
//iOS_Operation_ALL标签,那么只要装了这个app并且允许消息推送的用户就都能收到通知
[self setTags:&tags addTag:@"iOS_Operation_ALL"];
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
//版本号标签,根据版本号设置了iOSXX的标签 只有特定版本的用户才能收到通知
[self setTags:&tags addTag:[NSString stringWithFormat:@"iOS_%@",version]];
// 别名
__autoreleasing NSString *alias ;
if ([userModel.jobNumber length]) {
alias = [NSString stringWithFormat:@"%@", userModel.jobNumber];
}
[JPUSHService setTags:tags completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
if (iResCode == 0) { // 对应的状态码为0 ,代表成功 1011失败
NSLog(@"标签设置成功");
}
} seq:0];
[JPUSHService setAlias:alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
if (iResCode == 0) { // 对应的状态码为0 ,代表成功 1011失败
NSLog(@"别名设置成功");
}
} seq:0];
}
- (void)setTags:(NSMutableSet **)tags addTag:(NSString *)tag {
[*tags addObject:tag];
}
- 实现jpush的代理JPUSHRegisterDelegate
#pragma mark- delegate
//位于前台
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
} else {
// 本地通知 以后需要再用
}
completionHandler(UNNotificationPresentationOptionAlert);
// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
//位于后台
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
// NSDictionary * userInfo = response.notification.request.content.userInfo;
// UNNotificationRequest *request = response.notification.request; // 收到推送的请求
// UNNotificationContent *content = request.content; // 收到推送的消息内容
//
// NSNumber *badge = content.badge; // 推送消息的角标
// NSString *body = content.body; // 推送消息体
// UNNotificationSound *sound = content.sound; // 推送消息的声音
// NSString *subtitle = content.subtitle; // 推送消息的副标题
// NSString *title = content.title; // 推送消息的标题
NSDictionary *userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
// 这里可以做一些处理,用来响应从通知点击进来的操作
completionHandler(); // 系统要求执行这个方法
}
- 在AppDelegate中调用AppDelegate+Push.h里面的方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self pushApplication:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}