美文网首页
【原】 ios点击推送消息跳转到指定界面

【原】 ios点击推送消息跳转到指定界面

作者: 紫荆逸 | 来源:发表于2017-09-15 12:04 被阅读48次

    这里只粘贴了代码中重要跳转页面部分代码及当用户在浏览app的时候弹出消息提示(自定义弹出框)

    // 推送收到推送消息
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        // IOS 7 Support Required
        [JPUSHService handleRemoteNotification:userInfo];
        NSLog(@"收到通知:%@", [self logDic:userInfo]);
    
        //判断app是不是在前台运行,有三个状态(如果不进行判断处理,当你的app在前台运行时,收到推送时,通知栏不会弹出提示的)
        // UIApplicationStateActive, 在前台运行
        // UIApplicationStateInactive,未启动app
        //UIApplicationStateBackground    app在后台
    
        NSInteger badge=[userInfo[@"aps"][@"badge"] integerValue ];
        [[NSUserDefaults standardUserDefaults]setInteger:badge forKey:@"MesNum"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeNameNotification" object:self userInfo:@{@"name":@(badge)}];
    
        NSLog(@"角标:%ld",badge);
        if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
        {  //此时app在前台运行,我的做法是弹出一个alert,告诉用户有一条推送,用户可以选择查看或者忽略,自定义弹出框
    
    
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    
            msg=userInfo;
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,screen_width, 65)];
            bgView.backgroundColor=[UIColor blackColor];
            [window addSubview:bgView];
    
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ClickTap:)];
            [bgView addGestureRecognizer:tap];
    
            UIImageView *iconImv=[[UIImageView alloc]initWithFrame:CGRectMake(15, 5,18, 18)];
            iconImv.backgroundColor=[UIColor clearColor];
            iconImv.layer.cornerRadius=3;
            iconImv.layer.masksToBounds=YES;
            NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
            NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
            iconImv.image=[UIImage imageNamed:icon];
            [bgView addSubview:iconImv];
            UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(iconImv.frame)+7.5, CGRectGetMinY(iconImv.frame), 60, CGRectGetHeight(iconImv.frame))];
            titleLabel.backgroundColor=[UIColor clearColor];
            titleLabel.text=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
            titleLabel.textColor=[UIColor whiteColor];
            titleLabel.font=[UIFont boldSystemFontOfSize:12];
            [bgView addSubview:titleLabel];
            UILabel *timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame)+2.5, CGRectGetMinY(iconImv.frame), 100, CGRectGetHeight(iconImv.frame))];
            timeLabel.backgroundColor=[UIColor clearColor];
            timeLabel.textAlignment=NSTextAlignmentLeft;
            timeLabel.text=@"现在";
            timeLabel.textColor=[UIColor colorWithRed:0.402 green:0.478 blue:1.000 alpha:1.000];
            timeLabel.font=[UIFont boldSystemFontOfSize:10];
            [bgView addSubview:timeLabel];
            UILabel *alertLabel=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(iconImv.frame)+7.5, CGRectGetMinY(titleLabel.frame)+7.5, screen_width-CGRectGetMaxX(iconImv.frame)-15, CGRectGetHeight(bgView.frame)-CGRectGetMinY(titleLabel.frame)-2.5)];
            alertLabel.text=[NSString stringWithFormat:@"%@", userInfo[@"aps"][@"alert"]];
            alertLabel.font=[UIFont systemFontOfSize:12];
            alertLabel.numberOfLines=2;
            alertLabel.backgroundColor=[UIColor clearColor];
            alertLabel.textColor=[UIColor whiteColor];
            [bgView addSubview:alertLabel];
    
    
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alertLabel removeFromSuperview];
                [bgView removeFromSuperview];
                [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    
    
            });
    
    
            //        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"新消息" message:userInfo[@"aps"][@"alert"] preferredStyle:UIAlertControllerStyleAlert];
            //        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //        }];
            //        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //            [self goToMssageViewControllerWith:userInfo];
            //
            //        }];
            //        [alertController addAction:cancelAction];
            //        [alertController addAction:okAction];
            //        [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
            //
        }
        else {
            //这里是app未运行或者在后台,通过点击手机通知栏的推送消息打开app时可以在这里进行处理,比如,拿到推送里的内容或者附加      字段(假设,推送里附加了一个url为 www.baidu.com),那么你就可以拿到这个url,然后进行跳转到相应店web页,当然,不一定必须是web页,也可以是你app里的任意一个controll,跳转的话用navigation或者模态视图都可以
    
            [self goToMssageViewControllerWith:userInfo];
            //        mytabBar.selectedIndex=2;
    
    
    
        }
    
    
        completionHandler(UIBackgroundFetchResultNewData);
    
    
    }
    
    - (NSString *)logDic:(NSDictionary *)dic {
        if (![dic count]) {
            return nil;
        }
        NSString *tempStr1 =
        [[dic description] stringByReplacingOccurrencesOfString:@"\\u"
                                                     withString:@"\\U"];
        NSString *tempStr2 =
        [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
        NSString *tempStr3 =
        [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
        NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
        NSString *str =
        [NSPropertyListSerialization propertyListFromData:tempData
                                         mutabilityOption:NSPropertyListImmutable
                                                   format:NULL
                                         errorDescription:NULL];
        return str;
    }
    
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    
        //Optional
        NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
    }
    
    #pragma mark - 当运行在后台时,根据推送消息类型跳转界面、进行处理
    - (void)goToMssageViewControllerWith:(NSDictionary*)msgDic{
        NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
        [pushJudge setObject:msgDic forKey:@"push"];
        [pushJudge synchronize];
    //    [UIApplication sharedApplication].applicationIconBadgeNumber=0;
        [pushJudge setObject:@"" forKey:@"MesNum"];
    
    
        NSString *type=msgDic[@"msgtype"];
    
    
    
    
        //    if ([type isEqual:@"order"]) {//我的订单界面
        //
        //        QCMyOrderVC *orderVC=[[QCMyOrderVC alloc]initWithNibName:@"QCMyOrderVC" bundle:nil]; UINavigationController *mm=[[UINavigationController alloc]initWithRootViewController:orderVC];
        //        [self.window.rootViewController presentViewController:mm animated:YES completion:nil ];
        //    }else if ([type isEqual:@"car_report"])//车检报告界面
        //    {
        //        QCCheJianBaoGaoVC *orderVC=[[QCCheJianBaoGaoVC alloc]initWithNibName:@"QCCheJianBaoGaoVC" bundle:nil]; UINavigationController *mm=[[UINavigationController alloc]initWithRootViewController:orderVC];
        //        orderVC.push_Car=[NSString stringWithFormat:@"%@%@%@",msgDic[@"carBrand"],msgDic[@"carSeries"],msgDic[@"carModel"]];
        //        orderVC.push_cardId=msgDic[@"carId"];
        //        [self.window.rootViewController presentViewController:mm animated:YES completion:nil ];
        //
        //
        //    }else if ([type isEqual:@"activity"])//活动界面
        //    {
        //
        //
        //        NSString *urlString=msgDic[@"url"];
        //        NSURL*url=[NSURL URLWithString:urlString];
        //        [[UIApplication sharedApplication] openURL:url];
        //
        //
        //
        //    }else if ([type isEqual:@"coupon"])//优惠券界面
        //    {
        //        [self _IsNotFirst];
        //        self.mytabBar.selectedIndex=1;
        //    }else if ([type isEqual:@"refund_success"])//退款成功界面
        //    {
        //        QCCustomRecordVC *orderVC=[[QCCustomRecordVC alloc]initWithNibName:@"QCCustomRecordVC" bundle:nil];
        //        UINavigationController *mm=[[UINavigationController alloc]initWithRootViewController:orderVC];
        //        [self.window.rootViewController presentViewController:mm animated:YES completion:nil ];
        //
        //
        //    }else if ([type isEqual:@"refund_failed"])//退款失败
        //    {
        //        QCMyWalletVC *orderVC=[[QCMyWalletVC alloc]initWithNibName:@"QCMyWalletVC" bundle:nil];
        //        UINavigationController *mm=[[UINavigationController alloc]initWithRootViewController:orderVC];
        //        [self.window.rootViewController presentViewController:mm animated:YES completion:nil ];
        //
        //
        //    }
        //    else if ([type isEqual:@"version"])//版本相关
        //    {
        //        [self _IsNotFirst];
        //        self.mytabBar.selectedIndex=0;
        //
        //
        //    }
        //    else if ([type isEqual:@"comment"])//评论相关
        //    {
        //        QCMyOrderVC *orderVC=[[QCMyOrderVC alloc]initWithNibName:@"QCMyOrderVC" bundle:nil]; UINavigationController *mm=[[UINavigationController alloc]initWithRootViewController:orderVC];
        //        [self.window.rootViewController presentViewController:mm animated:YES completion:nil ];
        //
        //    }
    }
    
    
    -(void)ClickTap:(UITapGestureRecognizer*)tap
    {
        [self goToMssageViewControllerWith:msg];
        
        UIView *bgview=tap.view;
        [bgview removeFromSuperview];
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
        
    }
    
    + (void)showLocalNotificationAtFront:(UILocalNotification *)notification {
        [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
        return;
    }
    
    

    相关文章

      网友评论

          本文标题:【原】 ios点击推送消息跳转到指定界面

          本文链接:https://www.haomeiwen.com/subject/muessxtx.html