iOS 远程推送(极光推送) 根据后台推送内容的不同跳转指定页面(不断更新)

基本步骤就不再说了,可以谷歌
远程推送应用配置过程
一. 创建支持远程推送功能的App ID
二. 创建推送证书(开发证书和发布证书)和描述文件
三. 下载CER文件,并导入钥匙串管理
四. 我们需要重新生成一下配置文件
下面开始就介绍,点击推送的内容跳转指定页面
 现在点击推送消息,有两种跳转方式:
one.打开应用,跳转到应用首页;
默认的效果是点击推送消息,会直接进入应用,什么都不用设置,只要注册极光应用就可以。可以参考官方文档,写的非常详细,直接拷贝文档代码即可。
two.打开应用,跳转到指定页面。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [JPUSHService handleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);

    if (application.applicationState.applicationState == UIApplicationStateActive) {
        //程序运行时收到通知,先弹出消息框
        NSLog(@"程序在前台");
        [self popAlert:userInfo];


    }

    else{
        //程序已经关闭或者在后台运行
        [self pushToViewControllerWhenClickPushMessageWith:userInfo];
        //这里也可以发送个通知,跳转到指定页面
        // [self readNotificationVcWithUserInfo:userInfo];

    }

    [application setApplicationIconBadgeNumber:0];

    [JPUSHService handleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);
}
---------------
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification 
       withCompletionHandler:(void (^)(NSInteger))completionHandler {

    NSDictionary * userInfo = notification.request.content.userInfo;
    UNNotificationRequest *request = notification.request; // 收到推送的请求
    UNNotificationContent *content = request.content; // 收到推送的消息内容


    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];


        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
            //程序运行时收到通知,先弹出消息框
            NSLog(@"程序在前台");
            [self popAlert:userInfo];

        }

        else{
            //跳转到指定页面
            [self pushToViewControllerWhenClickPushMessageWith:userInfo];
            //这里也可以发送个通知,跳转到指定页面
            // [self readNotificationVcWithUserInfo:userInfo];

        }

    }
    // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}


-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response 
withCompletionHandler:(void (^)())completionHandler {
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    [[NSUserDefaults standardUserDefaults] setObject:@"Inactive" forKey:@"applicationState"];

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];


        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
            //程序运行时收到通知,先弹出消息框
            [self popAlert:userInfo];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"ApplicationState" object:@"0"];

        }

        else{

            [self pushToViewControllerWhenClickPushMessageWith:userInfo];
            //这里也可以发送个通知,跳转到指定页面
            // [self readNotificationVcWithUserInfo:userInfo];
        }

    }
    completionHandler();  // 系统要求执行这个方法
}
#endif

如果在程序运行时收到通知,这时消息栏不会显示通知,所以如果想让用户收到通知的话,应该是给用户一个弹框提醒,告诉用户有消息通知,当用户点击提示框中的确认查看按钮时,跳转到指定的页面
#pragma mark -- 程序运行时收到通知
-(void)popAlert:(NSDictionary *)pushMessageDic{

        UIAlertController *alertController = 
        [UIAlertController alertControllerWithTitle:@"" message:[[pushMessageDic objectForKey:@"aps"]objectForKey:@"alert"]
                                                                          preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *confirmAction = 
        [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [self pushToViewControllerWhenClickPushMessageWith:pushMessageDic];
                                                              }];

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


}

跳转到指定页面的方法。跳转到指定页面的话,可能会需要某些参数,这时可以根后台商定,根据参数跳转到相应的页面,同时也让后台把你需要的参数返回来。比如我和后台商定根据“type”这个参数确定跳转的页面。如果是跳转到次级页面,这里重要的是要找到正确的viewcontroller,用controller的nav进行push新页面。比如我的MessageViewController是用tabar的第一个item中的FirstViewController的nav进行push出来的,那么,当我点击通知消息想到跳转到MessageViewController,只要找到FirstViewController就可以了。

-(void)pushToViewControllerWhenClickPushMessageWith:(NSDictionary*)msgDic{


    NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];

            if ([[msgDic objectForKey:@"type"] integerValue]==0){

//      跳转到第一个tabbarItem,这时直接设置 UITabBarController的selectedIndex属性就可以

             self.tabController.selectedIndex = 0;

            }else if ([[msgDic objectForKey:@"type"] integerValue]==1){

                //跳转到第二个tabbarItem

                self.tabController.selectedIndex = 1;


            }else if ([[msgDic objectForKey:@"type"] integerValue]==2){
                //跳转到第三个tabbarItem


                self.tabController.selectedIndex = 2;

            }else if ([[msgDic objectForKey:@"type"] integerValue]==3){
                //详情,这是从跳转到第一个tabbarItem跳转过去的,所以我们可以先让tabController.selectedIndex =0;然后找到VC的nav。

                self.tabController.selectedIndex =0;
                MessageViewController * VC = [[MessageViewController alloc]init];
                [VC setHidesBottomBarWhenPushed:YES];
            //因为我用了三方全屏侧滑手势,所以要找的是第一个tabbarController中的viewController的JTNavigationController ,接着再找JTNavigationController 里面的jt_viewControllers.lastObject,这样就可以找到FirstViewController了,然后跳转的时候就和正常的跳转一样了
                JTNavigationController *nav=(JTNavigationController *)self.tabController.viewControllers[0];
                UIViewController *vc=(UIViewController*)nav.jt_viewControllers.lastObject;

                [vc.navigationController pushViewController:VC animated:NO];

            }else {

            }

//也可以发送通知跳转

- (void)readNotificationVcWithUserInfo:(NSDictionary *)userInfo {

    

    MessageViewController *vc = [[MessageViewController alloc] init];

    challengeVc.userInfo = userInfo;

    

    BaseNavigationController *challengeNaviVc = [[BaseNavigationController alloc] initWithRootViewController:challengeVc];

    

    [self.window.rootViewController presentViewController:vc animated:YES completion:nil];

}








  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值