首页 技术 正文
技术 2022年11月13日
0 收藏 378 点赞 4,704 浏览 4352 个字

在进行极光推送时候,发现版本有所更新,以前截取didfinish入口方法里面的launchOptions,获取一个本地的通知内容,进行本地展示不可用了,通过查询官方文档和网上的资料才发现,方法改变了,具体方法如下(只针对怎样定义消息):

1,功能说明

只有在前端运行的时候才能收到自定义消息的推送。

从jpush服务器获取用户推送的自定义消息内容和标题以及附加字段等。

2,实现方法

获取iOS的推送内容需要在delegate类中注册通知并实现回调方法。

在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions 加入下面的代码:

 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];

实现回调方法:

 - (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
NSString *content = [userInfo valueForKey:@"content"];
NSDictionary *extras = [userInfo valueForKey:@"extras"];
NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服务端传递的Extras附加字段,key是自己定义的 }

参数描述:

content:获取推送的内容

extras:获取用户自定义参数

customizeField1:根据自定义key获取自定义的value

更多实现参考 SDK下载压缩包中的 demo。

3,跳转目标页面方法的实现,在第二步的回调方法里面插入如下方法:

[self getPushMessageAtStateActive:userInfo];

方法代码:

#pragma mark -- 程序运行时收到通知
-(void)getPushMessageAtStateActive:(NSDictionary *)pushMessageDic{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
message:[pushMessageDic objectForKey:@"content"]
preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"查看"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UIViewController *targetVC = [self topVC:[UIApplication sharedApplication].keyWindow.rootViewController]; [targetVC.navigationController pushViewController:[[AuthenticationViewController alloc] init] animated:YES];
}]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:confirmAction];
[alertController addAction:cancelAction];
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];}
- (UIViewController *)topVC:(UIViewController *)rootViewController{
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tab = (UITabBarController *)rootViewController;
return [self topVC:tab.selectedViewController];
}else if ([rootViewController isKindOfClass:[UINavigationController class]]){
UINavigationController *navc = (UINavigationController *)rootViewController;
return [self topVC:navc.visibleViewController];
}else if (rootViewController.presentedViewController){
UIViewController *pre = (UIViewController *)rootViewController.presentedViewController;
return [self topVC:pre];
}else{
return rootViewController;
}
}

这样的话,就可以实现,点击弹出的alerview的确定按钮,进入指定的页面了!

————————————————————————————————————–

上面说的是发送自定义消息的方法,这里来说下app在后台,或者不活跃状态时候的操作:

1,通知的三种形式:

typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
UNNotificationPresentationOptionBadge = ( << ),
UNNotificationPresentationOptionSound = ( << ),
UNNotificationPresentationOptionAlert = ( << ),
}

2,在此方法里面实现:

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}else is
{
//本地通知
}
completionHandler(); // 系统要求执行这个方法
}

或者:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {    // Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"userinfo is %@",userInfo); UIViewController *targetVC = [self topVC:[UIApplication sharedApplication].keyWindow.rootViewController];
targetVC.hidesBottomBarWhenPushed = YES;
[targetVC.navigationController pushViewController:[[AuthenticationViewController alloc] init] animated:YES]; completionHandler(UIBackgroundFetchResultNewData);}

一个是系统的方法,一个是极光的方法,本地化创建一个alertview也是可以实现,也可以直接在系统方法里面,进行操作,比如跳转到指定页面之类的都可以的!

在目标页面的话,如果底下的tabbar没有隐藏的话,可添加如下代码(仅做参考,隐藏的方法有很多):

-(void)viewWillAppear:(BOOL)animated
{
self.tabBarController.tabBar.hidden = YES;
}-(void)viewWillDisappear:(BOOL)animated
{
self.tabBarController.tabBar.hidden = NO;
}

仅供参考,一些以官方文档为准!如果不正确的地方,欢迎指正!

附个极光的链接:

https://docs.jiguang.cn/jpush/client/iOS/ios_api/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898