问题
I'm implementing UILocalNotification
in my app, and the notifications are delivered as expected. As I have understood, the notifications are handled like this:
- When the application is not running, and the user click the notifiction, the app receives the notification through the options in the
applicationDidFinishLaunching
method. - When the application is active, it receives the notification through the
didReceiveLocalNotification
method. - 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?
回答1:
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.
来源:https://stackoverflow.com/questions/22728179/how-to-get-already-fired-uilocalnotification