如何设置 UILocalNotification 的过期时间?
我想显示一个 UILocalNotification
,但它的有效期只有 24 小时。 24 小时结束后,我希望通知“过期”;也就是说:用户无法再看到过时的通知。我该怎么做?
I would like to show a UILocalNotification
, but it will only be valid for 24 hours. At the end of 24 hours, I want the notification to "expire"; that is: the user can no longer see the obsolete notification. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要取消已发送的通知,请使用
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["myidentifier"])
或
此答案中的示例代码:https://stackoverflow.com/a/45218609/2650588
To cancel already delivered notifications use
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["myidentifier"])
or
Example code in this answer: https://stackoverflow.com/a/45218609/2650588
我指的是已经发出的通知。
我相信将徽章设置为零会清除现有通知。我将尝试看看这是否也会清除本地通知(除了推送通知之外)。
I'm referring to a notification that has already fired.
I believe that setting the badge to zero will clear out existing notifications. I will try to see whether this also clears local notifications (in addition to push notifications).
您可以使用 UIApplication.scheduledLocalNotifications 获取待处理通知的列表,然后取消您想要删除的通知。
You can use UIApplication.scheduledLocalNotifications to get a list of your pending notifications and then cancel the ones you want to get rid of.