删除特定的本地通知
我正在开发一个基于本地通知的 iPhone 闹钟应用程序。
删除警报后,相关的本地通知应被取消。但是我如何才能准确确定要取消本地通知数组中的哪个对象呢?
我知道 [[UIApplication sharedApplication] cancelLocalNotification:notification]
方法,但如何获取此“通知”来取消它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以在本地通知的用户信息中保存唯一的键值。
获取所有本地通知,循环遍历数组并删除特定通知。
代码如下,
OBJ-C:
SWIFT:
UserNotification:
如果你使用UserNotification (iOS 10+),只需按照以下步骤操作:
创建 UserNotification 内容时,添加唯一的 标识符
使用removePendingNotificationRequests(withIdentifiers:)
使用删除特定已发送的通知<一href="https://developer.apple.com/reference/usernotifications/unusernotificationcenter/1649500-removedeliverednotifications">removeDeliveredNotifications(withIdentifiers:)
有关详细信息,UNUserNotificationCenter
You can save a unique value for key in your local notification's userinfo.
Get all local notification, loop through the array and delete the particular notification.
Code as follows,
OBJ-C:
SWIFT:
UserNotification:
If you use UserNotification (iOS 10+), just follow this steps:
When creating the UserNotification content, add an unique identifier
Remove specific pending notification using removePendingNotificationRequests(withIdentifiers:)
Remove specific delivered notification using removeDeliveredNotifications(withIdentifiers:)
For more info, UNUserNotificationCenter
其他选项:
首先,当您创建本地通知时,可以将其存储在用户默认值中以供将来使用,本地通知对象不能直接存储在用户默认值中,需要先将该对象转换为 NSData 对象,然后 < code>NSData 可以存储到
用户默认值
中。下面是代码:在您存储并安排本地通知后,将来可能会要求您取消之前创建的任何通知,以便您可以从用户默认值中检索它。
希望这有帮助
Other Option:
First of All, when you create local notification, you can store it in user defaults for future use, Local notification object can not be stored directly in user defaults, This object needs to be converted into NSData object first, and then
NSData
can be stored intoUser defaults
. Below is code for that:After you have stored and scheduled local notification, In future, requirement may arise that you need to cancel any of notification that you created earlier, So you can retrieve it from User defaults.
Hope This helps
这就是我所做的。
创建通知时执行以下操作:
尝试删除通知时执行以下操作:
此解决方案应该适用于多个通知,并且您不管理任何数组、字典或用户默认值。您只需使用已保存到系统通知数据库中的数据即可。
希望这对未来的设计师和开发人员有所帮助。
祝各位编码愉快! :D
Here is what i do.
When creating your notification do this:
when trying to delete it do this:
This solution should work for multiple notifications, and your not managing any arrays or dictionaries or user defaults. Your simply using the data you've already saved to the systems notification database.
Hope this helps future designers and developers.
Happy coding guys! :D
快速调度和删除通知:
Scheduling and removeNotification in swift:
Swift 4 解决方案:
Swift 4 solution:
iMOBDEV 的解决方案可以完美地删除特定通知(例如删除警报后),但当您需要有选择地删除时它特别有用任何已触发且仍在通知中心的通知。
一种可能的情况是:警报通知触发,但用户打开应用程序而没有点击该通知并再次安排该警报。
如果您想确保通知中心只能显示给定项目/警报的一个通知,这是一个很好的方法。它还允许您不必在每次打开应用程序时清除所有通知,这应该更适合该应用程序。
NSKeyedArchiver
将其存储为UserDefaults
中的Data
。您可以创建一个与您在通知的 userInfo 字典中保存的键相同的键。如果它与 Core Data 对象关联,您可以使用其唯一的 objectID 属性。UserDefaults
上的密钥。下面是该解决方案的 Swift 3.1 版本(适用于 iOS 10 以下的目标):
存储
检索和删除
iMOBDEV's solution works perfectly to remove a specific notification (e.g. after deleting the alarm) but it's specially useful when you need to selectively remove any notification that has already fired and is still on the notification center.
A possible scenario would be: the notification for an alarm fires, but the user opens the app without tapping on that notification and schedules that alarm again.
If you want to make sure only one notification can be on the notification center for a given item/alarm, it's a good approach. It also allows you not having to clear all notifications every time the app is opened, shall that fit the app better.
NSKeyedArchiver
to store it asData
inUserDefaults
. You can create a key equal to what you're saving in the notification's userInfo dictionary. If it's associated with a Core Data object, you could use its unique objectID property.NSKeyedUnarchiver
. Now you're able to delete it using the cancelLocalNotification method.UserDefaults
accordingly.Here's a Swift 3.1 version of that solution (for targets below iOS 10):
Store
Retrieve and delete
Swift 版本,如果需要:
Swift Version, if need:
您可以在像这样安排通知时保留一个带有类别标识符的字符串
,然后搜索它并在需要时取消,像这样
You can keep a string with the category identifier when scheduling the notification like so
and search for it and cancel when needed like so
swift 3-style:
}
对于 iOS 10 使用:
swift 3-style:
}
for iOS 10 use:
您传递给
cancelLocalNotification:
的 UILocalNotification 对象将与具有匹配属性的任何现有 UILocalNotification 对象相匹配。因此:
将显示一个本地通知,稍后可以通过以下方式取消:
The UILocalNotification object you pass to
cancelLocalNotification:
will match any existing UILocalNotification object with matching properties.So:
will present a local notification that can later be cancelled with:
我在 Swift 2.0 中使用这个函数:
灵感来自@KingofBliss's Answer
I use this function in Swift 2.0:
Inspired from @KingofBliss's Answer
对于重复提醒(例如,您希望闹钟在周日、周六和周三下午 4 点触发,那么您必须设置 3 个闹钟并将 RepeatInterval 设置为 NSWeekCalendarUnit )。
用于制作仅一次提醒:
用于制作重复提醒:
用于过滤,您可以排列显示它。
要删除提醒,即使它是“仅一次”或“重复”:
For Repeated Reminders ( For example you want your alarm to fire on Sun, Sat and Wed at 4 PM , Then you have to make 3 alarms and set repeatInterval to NSWeekCalendarUnit ).
For making Once Only Reminder :
For Making Repeated Reminder :
For Filtering you array to display it.
To remove Reminder even it was Once Only or Repeated :
我对 KingofBliss 的答案进行了一些扩展,编写得更像 Swift2 一点,删除了一些不必要的代码,并添加了一些防撞装置。
首先,在创建通知时,您需要确保设置通知的
userInfo
的 uid(或任何自定义属性):然后,在删除它时,您可以执行以下操作:
I expanded on KingofBliss's answer a little, written this a little more Swift2-like, removed some unnecessary code, and added in some crash guards.
To start, when creating the notification, you need to make sure you set the uid (or any custom property really) of the notification's
userInfo
:Then, when deleting it, you can do:
删除已发送的通知 Swift5
Delete already delivered notification Swift5