I’ve read a number of questions here on SO regarding receiving push notifications while the application is not running (more than in the background, meaning it is shut down completely). This question in particular is most helpful in figuring out how to determine if one was receiving using the launchOptions dictionary.
However, I’m very confused, and I fully admit this may be a massive oversight on my part: when my device receives a push notification for this application while the app is shut down, and I later open my application, the launchOptions dictionary is a null pointer. From the description of the accepted answer in the previously mentioned link, and other places too, I gather that I should be able to see a notification payload; however there is nothing. I am developing for iOS 5.1.1.
My only other thought is to check the number of badges on start up (greater than zero, do something…), but this seems very unreliable.
Can anyone tell me what I am missing? Thank you in advance for your help!
application:didFinishLaunchingWithOptions:will only be called with payload information when app is launched due to notification. E.g. this could happen if user taps over notification alert (added in notification center) or notification received withcontent-avialble = 1in payload (Newsstand notification) & provided your app is not in foreground as well in background.If your app receives notification when app is in background. If it is Newsstand notification or if user taps over action button of alert below method is called
with
[[UIApplication sharedApplication] applicationState]not equal toUIApplicationStateActive.In above case if user does not tap over action button of notification alert and launch app by tapping over it, neither
didFinishLaunchingWithOptionsordidReceiveRemoteNotificationis called.If your app receives notification while in foreground
didReceiveRemoteNotificationis called[[UIApplication sharedApplication] applicationState]will be equal toUIApplicationStateActive.For badge in
notification, if your app is not running no code is executed and the badge is incremented by 1 in app icon. When you launch app (tap on app icon)didFinishLaunchingWithOptionsis called with normally. (If app is in background or foreground when notification received, same as above)So I think this covers every possible case. Also note that the background case is valid for
iOS SDK >= 4.0