关于杀掉进程点击推送消息和3Dtouch崩溃的问题

最近发现一个bug,就是在程序被杀掉之后,如果点击3DTouch或者推送消息刚进去就会崩溃.通过观察别的app,发现他们后台被杀掉之后点击推送消息或3DTouch进入程序,会跟正常启动app一样,先是广告页面或者欢迎界面,之后会在首页一个短暂的停顿后再push到具体界面.如图:


沿着这个思路继续思考,已知打开app不会调用didReceiveRemoteNotification方法,那么就需要在didFinishLaunchingWithOptions方法中获取通知内容,那么我们在AppDelegate里面设一个全局变量,用来接收推送消息和3DTouch的消息

@property (nonatomic, strong)NSDictionary *userinfo;

再在didFinishLaunchingWithOptions里面接收推送消息并保存在userinfo里面.

 NSDictionary * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        NSData *jsonData = [[userInfo objectForKey:@"ext"] dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                            options:NSJSONReadingMutableContainers
                                                              error:&err];
        _userinfo = [NSDictionary dictionaryWithDictionary:dic];
}

那么我们继续,点击推送后先进入欢迎页面再进入主页面,这个我是这么设计的,先把欢迎界面设为根视图,然后再在根视图里面有个block,在5秒钟后调用那个block把TabbarViewController设为根视图.代码如下
AppDelegate.m

#pragma mark -- 显示广告或者登陆界面
-(void)showMainBoard
{
    if (![KKUser user].token.length) {
        //如果用户没有登录过那么直接进入主页面,在主页面会present出登录界面
        NavController *nav = [MainStoryboard instantiateViewControllerWithIdentifier:@"nav"];
        [[KKUser user]clearSelf];
        self.window.rootViewController = nav;
    }else
    {
        //如果用户登录了,就先进入广告页面,然后再进入主界面
        LaunchController *vc = [MeStoryBoard instantiateViewControllerWithIdentifier:@"launch"];
        _window.rootViewController = vc;
        WEAKSELF
        [vc setLaunchblock:^{
            NavController *nav = [MainStoryboard instantiateViewControllerWithIdentifier:@"nav"];
            weakSelf.window.rootViewController = nav;
            if (_userinfo) {
                //如果有推送消息或者3DTouch消息的话会根据_userinfo的信息push到具体的界面
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [[JPushHelper defaultHelper]markMessageWithInfo:_userinfo];
                            [[JPushHelper defaultHelper]handleJumpingOfMessage];
                        });
            }
        }];
    }
}

那么好,我用极光推送后台测试一下,先把程序杀死再发送一条推送看看怎么样.(先是程序存在的情况,然后再把程序杀死看看)

推送

接下来就是3DTouch的处理了,如果程序已经启动了,那么根视图就应该是TabbarViewcontroller,而如果是把程序杀死再进入的话,那么根视图就应该是LaunchController(欢迎,广告界面).然后根据这个去判断,如果是前者那么直接根据点击的那个3DTouch按钮跳转到具体界面就行,而如果是后者的话,那么就将点击的信息保存到_userinfo中去,等5秒钟过后,欢迎界面中的block会自动处理_userinfo中的信息,就像推送一样.

-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    //这里可以实现界面跳转等方法
    if ([shortcutItem.type isEqualToString:@"item1"]) {
        if ([[[[[UIApplication sharedApplication]delegate]window]rootViewController] isKindOfClass:[LaunchController class]]) {
            _userinfo = @{@"type":@80};
        }else
        {
        MyNewsController *vc = [ProgramStoryBoard instantiateViewControllerWithIdentifier:@"mynews"];
        vc.hidesBottomBarWhenPushed = YES;
             [[JPushHelper defaultHelper]popToRoot];
        [[JPushHelper defaultHelper]jumpToVC:vc];
        }
    }
    else if ([shortcutItem.type isEqualToString:@"item2"])
    {
        if ([[[[[UIApplication sharedApplication]delegate]window]rootViewController] isKindOfClass:[LaunchController class]]) {
            _userinfo = @{@"type":@81};
        }else
        {
        [[JPushHelper defaultHelper]popToRoot];
        [appTabbarController changeSelectedItem:1];
        }
    }else if ([shortcutItem.type isEqualToString:@"item3"])
    {
        if ([[[[[UIApplication sharedApplication]delegate]window]rootViewController] isKindOfClass:[LaunchController class]]) {
            _userinfo = @{@"type":@82};
        }else
        {
        SearchController *vc = [ProgramStoryBoard instantiateViewControllerWithIdentifier:@"search"];
        vc.type = 1;
        vc.hidesBottomBarWhenPushed = YES;
             [[JPushHelper defaultHelper]popToRoot];
        [[JPushHelper defaultHelper]jumpToVC:vc];
        }
    }
}

看看效果吧,同样是对比.


3DTouch

这个易电有财是我在公司独立开发完成的项目,所以就直接拿来当示例了, 哈哈.
如果有不清楚的可以给我留言,有认为可以优化的地方也欢迎指出,谢谢大家!

你可能感兴趣的:(关于杀掉进程点击推送消息和3Dtouch崩溃的问题)