我在这里和其他地方看到过几个线程,但是似乎没有一个使用用于iOS 10的新的iOS框架。
有一个实例方法getDeliveredNotifications(completionHandler:)在UNUserNotificationCenter单例函数current()上调用。
completionHandler:接受一个传递通知数组,然后可以使用removeDeliveredNotifications(withIdentifiers:)在块内删除这些通知。
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
// UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [String])
}我的挑战是如何从所有交付的通知中识别特定的通知,然后删除它?
这就是我现在要做的,看看是否有一个远程通知,其中包含我从服务器发送的id,其中包含了有效载荷键ID。这并不会删除有问题的通知,显然是因为第一个函数返回零,尽管通知在通知中心是可见的。
func isThereANotificationForID(_ ID: Int) -> UNNotification? {
var foundNotification: UNNotification?
UNUserNotificationCenter.current().getDeliveredNotifications {
DispatchQueue.main.async {
for notification in notifications {
if notification.request.content.userInfo["id"] as! Int == ID {
foundNotification = notification
}
}
}
}
return foundNotification
}
func removeNotification(_ notification: UNNotification) {
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [notification.request.identifier])
}
// Find the notification and remove it
if let deliveredNotification = isThereANotificationForID(ID) {
removeNotification(deliveredNotification)
}发布于 2019-10-05 11:45:55
请注意,这个答案是使用Swift 5并在iOS 13上测试的。
我们可以扩展UNUserNotificationCenter以删除我们选择的通知。就我个人而言,我按线程对它们进行分组,并以这种方式删除它们,但是这个扩展还包括一种按字典对删除它们的方法。
extension UNUserNotificationCenter {
func decreaseBadgeCount(by notificationsRemoved: Int? = nil) {
let notificationsRemoved = notificationsRemoved ?? 1
DispatchQueue.main.async {
UIApplication.shared.applicationIconBadgeNumber -= notificationsRemoved
}
}
func removeNotifications(_ notifications: [UNNotification], decreaseBadgeCount: Bool = false) {
let identifiers = notifications.map { $0.request.identifier }
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers)
if decreaseBadgeCount {
self.decreaseBadgeCount(by: notifications.count)
}
}
func removeNotifications<T: Comparable>(whereKey key: AnyHashable, hasValue value: T, decreaseBadgeCount: Bool = false) {
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
let notificationsToRemove = notifications.filter {
guard let userInfoValue = $0.request.content.userInfo[key] as? T else { return false }
return userInfoValue == value
}
self.removeNotifications(notificationsToRemove, decreaseBadgeCount: decreaseBadgeCount)
}
}
func removeNotifications(withThreadIdentifier threadIdentifier: String, decreaseBadgeCount: Bool = false) {
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
let notificationsToRemove = notifications.filter { $0.request.content.threadIdentifier == threadIdentifier }
self.removeNotifications(notificationsToRemove, decreaseBadgeCount: decreaseBadgeCount)
}
}
func removeNotification(_ notification: UNNotification, decreaseBadgeCount: Bool = false) {
removeNotifications([notification], decreaseBadgeCount: decreaseBadgeCount)
}
}有了这个分机,您可以调用,例如,
UNUserNotificationCenter.current().removeNotifications(whereKey: "id", hasValue: 123)如果您还想减少应用程序图标上的徽章编号,可以设置decreaseBadgeCount: true,或者交替调用UNUserNotificationCenter.current().decreaseBadgeCount。
发布于 2018-09-26 11:59:14
以前的removeDeliveredNotifications不能正常工作。似乎苹果是固定的问题,因为它将通过使用单行代码为我工作。
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: aryIdentifier)我在模拟器里试了5-6个小时来清除本地通知,但是没有运气。当我将运行代码在实际的设备,它会喜欢的魅力。
注:请在真实设备上进行测试,以上方法不适用于模拟器。。
https://stackoverflow.com/questions/42518286
复制相似问题