简体   繁体   English

为什么收到推送通知时我的iOS应用程序崩溃?

[英]Why does my iOS app crash when receiving a push notification?

My iOS app is crashing when it receives a push notification message while running. 我的iOS应用在运行时收到推送通知消息时崩溃。 I'm using the sandbox APNS environment, and using Amazon SNS to send the APNS messages. 我正在使用沙盒APNS环境,并使用Amazon SNS发送APNS消息。

When debugging, I set a breakpoint on the first line of the following code snippet: 调试时,我在以下代码段的第一行上设置了一个断点:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
  NSDictionary *apnsPayload = [NSDictionary dictionaryWithDictionary:userInfo];

When the app receives a push notification, it hits the breakpoint and lets me debug; 当应用收到推送通知时,它会到达断点并让我进行调试; at this point I can see that userInfo is non-nil and contains the expected dictionary. 在这一点上,我可以看到userInfo为非nil并且包含预期的字典。

However, when I step through the code, the app crashes with EXC_BAD_ACCESS at the very next line—the assignment to apnsPayload . 但是,当我单步执行代码时,应用程序将在下一行EXC_BAD_ACCESS崩溃-分配给apnsPayload It seems like userInfo might be getting deallocated prematurely, but I'm not sure why, or more importantly how to change this. 似乎userInfo可能会过早地释放,但是我不确定为什么,或更重要的是如何更改它。

I don't think it makes sense to turn the NSDictionary into another NSDictionary with [NSDictionary dictionaryWithDictionary:] ... also NSDictionary can be non-nil but contain 0 key entries (an empty dictionary). 我认为使用[NSDictionary dictionaryWithDictionary:]将NSDictionary转换为另一个NSDictionary没有意义。NSDictionary也可以为非nil,但包含0个键条目(空字典)。

Perhaps you want: NSDictionary *apnsPayload = [userInfo objectForKey: @"alert"]; 也许您想要:NSDictionary * apnsPayload = [userInfo objectForKey:@“ alert”]; ?

See: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html 参见: https : //developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

Well, I've still no idea why this is happening, but it stops when I NSLog() the userInfo argument first. 好吧,我仍然不知道为什么会这样,但是当我首先使用NSLog() userInfo参数时,它就停止了。 Adding the following line as the first line of the function prevents the crash: 将以下行添加为函数的第一行可防止崩溃:

  NSLog(@"Received APNS with userInfo %@", userInfo);

I can then assign using - [userInfo objectForKey:] without causing a crash. 然后,我可以使用- [userInfo objectForKey:]进行分配,而不会导致崩溃。 (To be clear, attempting this same assignment without the prior NSLog() results in the EXC_BAD_ACCESS crash.) (很明显,在没有先前的NSLog()情况下尝试进行相同的分配会导致EXC_BAD_ACCESS崩溃。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在接收UIApplicationDidReceiveMemoryWarningNotification时iOS应用会崩溃 - why does iOS app crash when receiving UIApplicationDidReceiveMemoryWarningNotification 当应用程序在后台接收推送通知时,应用程序崩溃 - When App in Background and receiving push notification tap App goes crash 当应用程序在前台时,IOS Play Sound接收推送通知 - IOS Play Sound on receiving push notification when app in foreground 尝试presentModalViewController时,为什么我的iOS应用程序崩溃? - Why does my iOS app crash when trying presentModalViewController? 为什么我的 iOS 应用打开后突然崩溃? - Why does my iOS app suddenly start to crash when opened? ios-在应用程序处于活动状态时打开推送通知时,它会导致崩溃 - ios - when opening a push notification while the app is active, it cases a crash App在前景上并收到推送通知消息(IOS)时发生钛合金崩溃 - Titanium Crash when App is on Foreground and receives a Push Notification message (IOS) iOS推送通知点击导致应用崩溃 - iOS push notification click causes app to crash 收到推送通知时,iPhone App崩溃 - iPhone App crashes when receiving push notification 使用推送通知启动时应用程序崩溃 - App crash when launch with a push notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM