zoukankan      html  css  js  c++  java
  • ios 开发之 -- 极光推送,发送自定义消息,进入制定页面

    在进行极光推送时候,发现版本有所更新,以前截取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   = (1 << 0),
        UNNotificationPresentationOptionSound   = (1 << 1),
        UNNotificationPresentationOptionAlert   = (1 << 2),
    } 

    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/#api-ios

  • 相关阅读:
    产品经理之路(八)
    产品经理之路(七)
    产品经理之路(六)
    产品经理之路(五)
    产品经理之路(四)
    产品经理之路(三)
    产品经理之路(二)
    react-admin-plus 正式开源, 欢迎star
    uni-app—从安装到卸载
    vue项目使用websocket技术
  • 原文地址:https://www.cnblogs.com/hero11223/p/7002677.html
Copyright © 2011-2022 走看看