How to get already fired UILocalNotification

允我心安 提交于 2019-12-10 08:12:38

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!