应用程序未运行时处理远程通知时崩溃 [英] Crash when handling remote notification when app not running

查看:28
本文介绍了应用程序未运行时处理远程通知时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到远程通知,并根据通知类型更改导航控制器的视图控制器.

I receive a remote notification and according to the type of notification, change navigation controller's view controllers.

当应用程序在前台时,或者当应用程序在后台但未完全关闭(从多任务栏)时,一切正常.

It all works fine when the app is in the foreground, or when the app is in the background but not completely closed (from multi-tasking bar).

但是,当应用程序关闭并收到远程通知时,它会在打开时立即崩溃.我在设置 ViewControllers 的方式上做错了吗?

But, when the app is closed, and receives a remote notification it crashes as soon as it opens. Am I doing wrong with the way I am setting up the ViewControllers?

这是一些代码.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
   // Push required screens into navigation controller

         UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

    //Accept push notification when app is not open
    if (remoteNotif) {      
        [self handleRemoteNotification:application userInfo:remoteNotif.userInfo];
        return YES;
    }

    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

-(void) handleRemoteNotification:(UIApplication *)application userInfo:(NSDictionary *)userInfo {
    application.applicationIconBadgeNumber = 0;

NSMutableArray *viewControllers = [NSMutableArray array];
    [viewControllers addObject:driverWaitViewController];
    [viewControllers addObject:newJobsViewController];

    [navigationController setViewControllers:viewControllers];
}

推荐答案

我已经解决了这个问题,我认为它与视图控制器无关.

I got this resolved, and it has nothing to do with view controllers, as I thought.

问题出在以下几行.我发送的是 remoteNotif.userInfo 而不是 remoteNotif 本身.此外,remoteNotif 显然不是 UILocalNotification 类型.它是一个 NSDictionary 对象.

The issue was in the following lines. I was sending in remoteNotif.userInfo rather than remoteNotif itself. Also, remoteNotif is obviously not of type UILocalNotification. It is a NSDictionary object.

之前

UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

[self handleRemoteNotification:application userInfo:remoteNotif.userInfo];

应该

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

[self handleRemoteNotification:application userInfo:remoteNotif];

这篇关于应用程序未运行时处理远程通知时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆