最近项目集成推送功能,用的极光推送的最新版本3.0.2较于之前,代理方法与17年之前稍有不同,我把整体的集成步骤及推送功能实现,都po在这里,可做借鉴
功能实现: @在登录状态下,才可推送消息成功,退出登录状态,不可推送消息
@应用程序在后台挂起状态下,可实现推送,进入相应界面
@应用程序在前台情况下,无论任何界面,可实现推送给,并进入相应界面
@应用程序被杀死,但登录过状态下,还能收到消息
需要在登录时,设置alias,退出登录时,清除alias
创建成功后自动生成 AppKey 用以标识该应用
集成sdk,加入相应的框架,可参照极光文档(集成指南)
static NSString *appKey = @"50752c2688d9697958a5f474"; //推送的appkey
static NSString *channel = @"App Store";
static BOOL isProduction = FALSE; //开发环境, NO则为生产环境
@property (nonatomic,copy)NSString * registerid; //用户推送的regist_id
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式 JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 可以添加自定义categories
// NSSet*categories for iOS10 or later
// NSSet*categories for iOS8 and iOS9
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
//初始化JPush
// 获取IDFA
// 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
// NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
// init Push
// notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil
// 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:nil];
//app未运行时,收到推送消息
NSDictionary *resultDic = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (resultDic) {//推送进入APP
NSLog(@"app未启动,推送进入,直接显示预警界面");
[self SetMainTabbarController2]; //相应界面跳转方法
}else{//正常进入APP
}
return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken]; //如想获得,devicetoken直接转换成字符串即可
//获得注册后的regist_id,此值一般传给后台做推送的标记用,先存储起来
_registerid = [JPUSHService registrationID];
}
// 添加处理APNs通知回调方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
NSLog(@"收到的推送消息 userinfo %@",userInfo);
//判断应用是在前台还是后台
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
// 前台收到消息后,做的对应页面跳转操作
[self SetMainTabbarController2];
// [[NSNotificationCenter defaultCenter]postNotificationName:@"REFRESHYUJING" object:nil];
NSLog(@"前台收到消息");
}
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
// 通过点击推送弹出的通知调用,包括前台和后台(didReceiveNotificationResponse)
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary *userInfo = response.notification.request.content.userInfo;
if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
[self SetMainTabbarController2]; //收到推送消息,需要调整的界面
// 消息界面监听刷新
// [[NSNotificationCenter defaultCenter] postNotificationName:ReceiveMessageNotification object:nil];
// [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
completionHandler(); // 系统要求执行这个方法
}
上述方法,都在appdelegate.m中实现。登录成功后,提交自身的register_id值给后台,并设置别名提交给极光推送服务器
[JPUSHService setAlias:userphone completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
//设置别名成功后,code值为0
NSLog(@"++++++++rescode: %ld, \ntags: %@, \nalias: %@\n", (long)iResCode, @"tag" , iAlias);
} seq:0];
//提交register_id给后台(此方法为后台提供)
[self giveRegisterId:appdele.registerid andToken:dict[@"token"]];
//删除推送的alias
[JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
NSLog(@"rescode: %ld, \ntags: %@, \nalias: %@\n", (long)iResCode, @"tag" , iAlias);
} seq:0];