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

你可能感兴趣的:(IOS:APP三种状态下收到推送后的跳转操作)