Calrion
Calrion

Reputation: 3332

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. I'm using the sandbox APNS environment, and using Amazon SNS to send the APNS messages.

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.

However, when I step through the code, the app crashes with EXC_BAD_ACCESS at the very next line—the assignment to apnsPayload. It seems like userInfo might be getting deallocated prematurely, but I'm not sure why, or more importantly how to change this.

Upvotes: 0

Views: 1783

Answers (2)

Calrion
Calrion

Reputation: 3332

Well, I've still no idea why this is happening, but it stops when I NSLog() the userInfo argument first. 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. (To be clear, attempting this same assignment without the prior NSLog() results in the EXC_BAD_ACCESS crash.)

Upvotes: 0

diclophis
diclophis

Reputation: 2442

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

Perhaps you want: NSDictionary *apnsPayload = [userInfo objectForKey: @"alert"]; ?

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

Upvotes: 0

Related Questions