IOS:APP三种状态下收到推送后的跳转操作

IOS推送总结:
1.直接打开APP时,调用didFinishLaunchingWithOptions方法,即使有推送,获取好的推送数量也为0,即:不会执行跳转方法;
2.点击推送(通知栏)打开APP时,调用didFinishLaunchingWithOptions方法,获取到推送的数量不为0,会执行跳转方法;
3.APP在后台运行时,点击推送(通知栏),或者直接再次打开APP时,调用didReceiveRemoteNotification方法,执行跳转方法;
4.APP在前台运行时,收到推送,调用didReceiveRemoteNotification方法,执行跳转方法;

备注:
1.本文不涉及如何建立推送,只写收到推送后如何跳转

#import "AppDelegate.h"

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 省略...
    //苹果推送注册
    [[GTNotificationUtils getInstance] registerRemoteNotification];
    // 通知跳转,app通过点击顶部推送启动app时,获取推送,如果不为空,则执行推送方法
    NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remoteNotification) {// 如果通知不为空,则证明收到了推送
        [self noticeClickDealWithUserInfo:remoteNotification isLaunch:YES];
    }
}

// app打开或者处于后台运行时收到通知后执行调用该代理方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    [self noticeClickDealWithUserInfo:userInfo isLaunch:NO];
}

// private method
- (void)noticeClickDealWithUserInfo:(NSDictionary *)userInfo isLaunch:(BOOL)isLaunch{// 推送跳转处理
    NSString *action = [GTAppUtils filterNull:userInfo[@"custom"][@"action"]];
    if (!action.length) {
        return;
    }
    
    UIViewController *vc = self.window.rootViewController;
    UINavigationController *nvc;
    UITabBarController *tvc;
    if ([vc isKindOfClass:[UITabBarController class]]) {
        tvc = (UITabBarController *)vc;
        nvc = tvc.selectedViewController;
    }else{
    }
    
    NSString *title = userInfo[@"aps"][@"alert"][@"title"];
    NSString *content = userInfo[@"aps"][@"alert"][@"body"];
    
    void (^pushBlock)() = ^{
        if ([action isEqualToString:@"1"]) {// 埋单
            tvc.selectedIndex = 2;
        }else if ([action isEqualToString:@"2"]){// 预警
            tvc.selectedIndex = 2;
        }else if ([action isEqualToString:@"3"]){// 止盈止损
            tvc.selectedIndex = 2;
        }else if ([action isEqualToString:@"6"] || [action isEqualToString:@"7"] ||[action isEqualToString:@"8"] || [action isEqualToString:@"5"]){// 文章(实时资讯,财经要闻,财经日历),评论
            GTArticleViewController *articleVC = [[GTArticleViewController alloc] init];
            NSMutableDictionary *articleInfo = [NSMutableDictionary dictionary];
            [articleInfo setValue: [GTAppUtils filterNull:userInfo[@"custom"][@"face_section_name"]] forKey:@"face_section_id"];
            [articleInfo setValue:[GTAppUtils filterNull:userInfo[@"custom"][@"article_id"]] forKey:@"article_id"];
            [articleInfo setValue:[GTAppUtils filterNull:userInfo[@"custom"][@"file_url"]] forKey:@"file_url"];
            articleVC.articleInfoDict = articleInfo;
            articleVC.hidesBottomBarWhenPushed = YES;
            [nvc pushViewController:articleVC animated:YES];
        }else if ([action isEqualToString:@"4"]){// 客服消息
            if ([[nvc.viewControllers lastObject] isKindOfClass:[GTServicerDetailViewController class]]) {
                return;
            }
            if([[GTUserManager shareManager] judgeIfLogin]){
                GTServicerDetailViewController *serviceVC = [[GTServicerDetailViewController alloc] init];
                serviceVC.hidesBottomBarWhenPushed = YES;
                [nvc pushViewController:serviceVC animated:YES];
            }else{
                GTLoginViewController *loginVC = [[GTLoginViewController alloc] init];
                loginVC.successBlock = ^{
                    GTServicerDetailViewController *serviceVC = [[GTServicerDetailViewController alloc] init];
                    serviceVC.hidesBottomBarWhenPushed = YES;
                    [nvc pushViewController:serviceVC animated:YES];
                };
                [nvc pushViewController:loginVC animated:YES];
            }
        }else if ([action isEqualToString:@"0"]){// 系统消息
        }
    };
    
    if (!isLaunch) {// 后台或者前台时给出提示弹窗
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:content preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            pushBlock();
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alertVC addAction:action1];
        [alertVC addAction:action2];
        UIViewController *rootVC = [[[UIApplication sharedApplication] delegate] window].rootViewController;
        [rootVC presentViewController:alertVC animated:YES completion:nil];
    }else{// 如果是打开APP,则直接跳转
        pushBlock();
    }
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 推送通知 注意:这里说的推送通知跟NSNotification有所区别 NSNotification是抽象的,不可...
    iOS开发攻城狮阅读 9,775评论 1 13
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 11,722评论 10 16
  • 推送通知注意:这里说的推送通知跟NSNotification有所区别NSNotification是抽象的,不可见的...
    醉叶惜秋阅读 5,435评论 0 3
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,235评论 25 709
  • 2016年10月21日 By 紫微lotus 在生活现实中,操练接纳的技能 昨儿孩子百天,妈妈从老家坐火车来到西安...
    作家阿紫阅读 2,854评论 2 1

友情链接更多精彩内容