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

标签 iphone objective-c push-notification apple-push-notifications

我收到远程通知并根据通知类型更改导航 Controller 的 View Controller 。

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

但是,当应用程序关闭并收到远程通知时,它会在打开后立即崩溃。我设置 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];
}

最佳答案

我已经解决了这个问题,正如我所想的那样,它与 View Controller 无关。

问题出在以下几行中。我发送的是 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];

关于iphone - 应用程序未运行时处理远程通知时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4295122/

相关文章:

ios - scrollViewDidScroll : when scroll programmatically?

ios - 在 UIScrollView 中使用 ViewController

iOS - 检查应用程序中的推送通知支持

android - GCM,用户通知,同一设备上的两个 GCM 感知应用程序,当 GCM 消息与通知 key 一起发送时触发哪个应用程序?

ios - Parse 和 Facebook 的问题

ios - 未调用嵌套 UITextField 的 UITextFieldDelegate

objective-c - 如何创建可变函数? (可以接受任意数量的参数)

node.js - 将 Google 推送通知结果设置为错误 401 未经授权的 WebHook 回调 channel

iphone - 很难捕获 UIButton 子类上的双击。 (捕获双击的时间延迟)

iphone - 使用 UIBarButtonItem 将图像添加到工具栏时出现问题,显示空白框而不是图像