如何获得已经被解雇的 UILocalNotification [英] How to get already fired UILocalNotification

查看:53
本文介绍了如何获得已经被解雇的 UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用中实现 UILocalNotification,并且通知按预期传递.据我了解,通知的处理方式如下:

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. 当应用未运行时,用户点击通知,应用通过 applicationDidFinishLaunching 方法中的选项接收通知.
  2. 当应用程序处于活动状态时,它通过 didReceiveLocalNotification 方法接收通知.
  3. 当应用程序在后台,并且用户点击通知时,它通过didReceiveLocalNotification方法接收通知.
  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.

但有一种情况我没能涵盖,那就是当一个通知被触发时,应用程序在后台,只需点击应用程序图标(而不是点击通知)就可以激活应用程序).在这种情况下,没有可用的 UILocalNotification.但我仍然想知道自上次活动以来通知已被触发,因为通知在其 userInfo 属性中有一些信息需要处理.

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.

目前,我通过检查 applicationDidBecomeActive 中的徽章编号 > 0 来解决"它,但这仅允许我向用户显示一般消息.我希望能够知道是什么通知导致徽章编号更新.有没有办法实现这一目标?

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?

推荐答案

几天前我遇到了完全相同的问题.幸运的是,有一种相当简单的方法可以解决这种情况!

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

将通知保存在 NSUserDefaults 中,使用 NSKeyedArchiver 将其转换为 NSData.如果您使用通知的 userInfo 字典中存储的相同密钥,这可能会更容易.

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.

使用 NSKeyedUnarchiver 将其作为 UILocalNotification 取回.然后,您可以根据需要处理通知(或使用 cancelLocalNotification 方法将其删除).

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).

此处进一步解释.

这篇关于如何获得已经被解雇的 UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆