[英]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.