应用程序未运行时处理远程通知时崩溃

发布于 2024-10-04 23:32:14 字数 1160 浏览 5 评论 0原文

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

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

但是,当应用程序关闭并收到远程通知时,它一打开就会崩溃。我设置 ViewController 的方式是否错误?

这是一些代码。

- (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 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).

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?

Here's some code.

- (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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

通知家属抬走 2024-10-11 23:32:14

我解决了这个问题,正如我所想,它与视图控制器无关。

问题如下。我发送的是remoteNotif.userInfo 而不是remoteNotif 本身。另外,remoteNotif 显然不是 UILocalNotification 类型。它是一个 NSDictionary 对象。

之前

UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

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

应该

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

[self handleRemoteNotification:application userInfo:remoteNotif];

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

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.

Before

UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

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

Should be

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

[self handleRemoteNotification:application userInfo:remoteNotif];
莫相离 2024-10-11 23:32:14

如果您关闭从 xcode 调试模式启动的应用程序,并且当应用程序以推送通知启动(关闭的应用程序)时,如果您的手机连接到 mac(您的手机仍然处于 xcode 调试模式),它将崩溃。用拔掉手机的电源来测试这个场景。

if you close the app which start from xcode debug mode, and when the app start with push notification(closed app) if the your phone connected to mac(still your phone in debug mode with xcode) it will be crash. test this senario with unplugged phone.

梅窗月明清似水 2024-10-11 23:32:14

收到通知时,您没有正确初始化您的应用程序。
将 application:didFinishLaunchingWithOptions: 方法更改为:

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

   NSDictionary *notif= [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

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

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

   return YES;
}

You aren't properly initializing your application when receiving a notification.
Change the application:didFinishLaunchingWithOptions: method to this:

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

   NSDictionary *notif= [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

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

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

   return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文