5

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!

Community
  • 1
  • 1
Paul Richter
  • 10,908
  • 10
  • 52
  • 85
  • What is inside payload ? – msk Aug 29 '12 at 21:21
  • @MSK I'm sorry I don't quite understand - are you asking what the original push (from my server) contained as its payload? If so, the payload is basic, it does not contain anything custom beyond the usual badge, alert, and sound. – Paul Richter Aug 29 '12 at 21:24

1 Answers1

8

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 with content-avialble = 1 in 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

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

with [[UIApplication sharedApplication] applicationState] not equal to UIApplicationStateActive.

In above case if user does not tap over action button of notification alert and launch app by tapping over it, neither didFinishLaunchingWithOptions or didReceiveRemoteNotification is called.

If your app receives notification while in foreground didReceiveRemoteNotification is called [[UIApplication sharedApplication] applicationState] will be equal to UIApplicationStateActive.

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) didFinishLaunchingWithOptions is 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

Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
msk
  • 8,885
  • 6
  • 41
  • 72
  • Ah ha, thank you for your answer. My main goal was to fetch some data from a server when a push is received; we have this working in the background and foreground, and I was hoping I could detect, on load, if a push was received while the app was offline; it appears, based on your answer, that this is impossible unless the user touches the notification bar. In your opinion, is it reliable to check the badge count on-load? – Paul Richter Aug 29 '12 at 21:54
  • you have to maintain badge count in your server see [this SO question](http://stackoverflow.com/questions/1942605/push-notification-badge-auto-increment) – msk Aug 30 '12 at 02:53
  • Ah yeah cool, I already knew about that one. That's not quite what I was asking though: I know that I can check, within the app, the badge count using [application applicationIconBadgeNumber]. What I was wondering if whether it is reliable to check if the badge count is greater than zero (`[application applicationIconBadgeNumber] > 0`). Is there any reason why I should **not** do this? – Paul Richter Aug 30 '12 at 14:56
  • yes you can check that. See [this](http://blog.parse.com/2012/07/18/badge-management-for-ios/) if it is related or of any help. – msk Aug 30 '12 at 16:50