[英]cancelAllLocalNotifications not working in iOS10
我想在添加新通知时从NotificationCenter中删除所有以前的本地通知。 但它适用于iOS9.0及更低版本,但在iOS 10中它会触发多个本地通知。 所以似乎cancelAllLocalNotifications
没有清除通知。
代码在iOS10中成功编译。
UIApplication.shared.cancelAllLocalNotifications()
对于iOS 10,Swift 3.0
从iOS 10弃用了cancelAllLocalNotifications
。
@available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]") open func cancelAllLocalNotifications()
你必须添加这个import语句,
import UserNotifications
获取通知中心。 并执行如下操作
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
如果要删除单个或多个特定通知,可以通过以下方法实现。
center.removeDeliveredNotifications(withIdentifiers: ["your notification identifier"])
希望能帮助到你..!!
对于iOS 10,目标C :
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllDeliveredNotifications];
[center removeAllPendingNotificationRequests];
斯威夫特4 :
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
如果要列出所有通知:
func listPendingNotifications() {
let notifCenter = UNUserNotificationCenter.current()
notifCenter.getPendingNotificationRequests(completionHandler: { requests in
for request in requests {
print(request)
}
})
}
对于iOS 10,您可以使用这种方式删除所有本地通知。 我刚刚测试过,它正在工作。
NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;
for (UILocalNotification *localNotification in arrayOfLocalNotifications) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification] ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.