4

I'm implementing UILocalNotification in my app, and the notifications are delivered as expected. As I have understood, the notifications are handled like this:

  1. When the application is not running, and the user click the notifiction, the app receives the notification through the options in the applicationDidFinishLaunching method.
  2. When the application is active, it receives the notification through the didReceiveLocalNotification method.
  3. When the application is in the background, and the user clicks the notification, it receives the notification through the didReceiveLocalNotification method.

But there is one case I have not been able to cover, and that is when a notification has been fired, the application is in the background, and the application is simply activated by clicking the app icon (instead of clicking the notification). In this case, there is no UILocalNotification available. But I still would like to know that a notification has been fired since the last time it was active, since the notification has some information to be acted upon in it's userInfo property.

Currently I have "solved" it by checking that the badge number is > 0 in applicationDidBecomeActive, but this just allows me to show a general message to the user. I would prefer to be able to know what notification caused the badge number to update. Is there some way to achieve this?

NilsH
  • 13,705
  • 4
  • 41
  • 59
  • Hi NilsH, did you end up finding a solution to this? – Tim Nov 12 '14 at 03:55
  • 1
    No, sorry. This seem not to be possible without storing them by yourself somehow. I just switched to remote notifications instead. – NilsH Nov 12 '14 at 08:12

1 Answers1

0

I had the exact same problem a few days ago. Luckily there's a rather simple way to cover that case!

Save the notification in NSUserDefaults, using NSKeyedArchiver to convert it to NSData. It's probably easier if you use the same key stored in the notification's userInfo dictionary.

Use NSKeyedUnarchiver to get it back as UILocalNotification. Then you're able to handle the notification as you see fit (or delete it using the cancelLocalNotification method).

Further explained here.

Community
  • 1
  • 1
Rygen
  • 309
  • 4
  • 15