【发布时间】:2017-02-28 20:38:14
【问题描述】:
我在这里和其他地方看到了几个关于此的主题,但似乎没有一个正在使用适用于 iOS 10 的新 UserNotifications 框架
有一个实例方法getDeliveredNotifications(completionHandler:)在UNUserNotificationCenter单例函数current()上调用
completionHandler: 接收一组传递的通知,然后可以使用 removeDeliveredNotifications(withIdentifiers:) 在块内删除这些通知
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
// UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [String])
}
我的挑战是如何从所有已发送的通知中识别特定通知,然后将其删除?
这就是我现在正在做的事情,看看是否有一个远程通知,其中包含我从服务器发送的带有有效负载密钥ID 的 id。这不会删除有问题的通知,显然是因为第一个函数返回 nil 尽管通知在通知中心可见。
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)
}
【问题讨论】:
-
有什么解决办法吗?
-
@Ahmed Khedr 如果您有解决方案。请在这里发帖