iOS10 之后,集成极光推送功能

最近项目集成推送功能,用的极光推送的最新版本3.0.2较于之前,代理方法与17年之前稍有不同,我把整体的集成步骤及推送功能实现,都po在这里,可做借鉴

功能实现: @在登录状态下,才可推送消息成功,退出登录状态,不可推送消息

@应用程序在后台挂起状态下,可实现推送,进入相应界面

@应用程序在前台情况下,无论任何界面,可实现推送给,并进入相应界面

@应用程序被杀死,但登录过状态下,还能收到消息

需要在登录时,设置alias,退出登录时,清除alias

1.在 JPush的管理Portal 上创建应用并上传APNs证书。

创建成功后自动生成 AppKey 用以标识该应用

集成sdk,加入相应的框架,可参照极光文档(集成指南)

2.添加头文件到 AppDelegate.m中

static NSString *appKey = @"50752c2688d9697958a5f474"; //推送的appkey

static NSString *channel = @"App Store";

static BOOL isProduction = FALSE; //开发环境, NO则为生产环境

@property (nonatomic,copy)NSString * registerid; //用户推送的regist_id

3.初始化APNs(JPush的注册,初始化都放在此代理方法中实现,app未开启状态下的推送,也会走此代理方法)

- (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;

}

4.注册APNs成功并上报DeviceToken给极光服务器 (此代理方法,是本机注册成功后,回调到程序的代理方法)

- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

/// Required - 注册 DeviceToken

[JPUSHService registerDeviceToken:deviceToken]; //如想获得,devicetoken直接转换成字符串即可

//获得注册后的regist_id,此值一般传给后台做推送的标记用,先存储起来

_registerid = [JPUSHService registrationID]; 

}

5.  // 通过点击推送弹出的通知调用,包括前台和后台iOS 10 Support (willPresentNotification)

// 添加处理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();  // 系统要求执行这个方法

}

6.  设置别名,提交获得的register_id给后台

上述方法,都在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"]];

7.在推出登录部分,清除别名,实现推出登录状态获取不到推送消息

//删除推送的alias

[JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {

NSLog(@"rescode: %ld, \ntags: %@, \nalias: %@\n", (long)iResCode, @"tag" , iAlias);

} seq:0];



作者:Hehe_99e3
链接:https://www.jianshu.com/p/6cca682a2892
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值