应用程序未运行时处理远程通知时崩溃
Posted
技术标签:
【中文标题】应用程序未运行时处理远程通知时崩溃【英文标题】:Crash when handling remote notification when app not running 【发布时间】:2010-11-28 03:07:22 【问题描述】:我收到一个远程通知,并根据通知类型更改导航控制器的视图控制器。
当应用程序在前台,或者当应用程序在后台但未完全关闭(从多任务栏)时,一切正常。
但是,当应用程序关闭并收到远程通知时,它会在打开时立即崩溃。我在设置 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];
【问题讨论】:
【参考方案1】:我已经解决了这个问题,我认为它与视图控制器无关。
问题在于以下几行。我发送的是 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];
【讨论】:
【参考方案2】:如果您关闭从 xcode 调试模式启动的应用程序,并且当应用程序以推送通知(关闭的应用程序)启动时,如果您的手机连接到 mac(您的手机仍处于使用 xcode 的调试模式),它将崩溃。用不插电的手机测试这个场景。
【讨论】:
这暂时解决了我的问题,但这是为什么呢?!对我来说没有意义 正如你所说,对我来说也没有意义。但我认为这里与设备和 mac 有一些联系。【参考方案3】:您在收到通知时没有正确初始化您的应用程序。 将 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;
【讨论】:
感谢您的回复 Vakio。问题是别的东西。请看看我是怎么解决的。 是的,我认为这很奇怪,但我没有追上它。对不起。以上是关于应用程序未运行时处理远程通知时崩溃的主要内容,如果未能解决你的问题,请参考以下文章