添加新通知时,我希望从NotificationCenter中删除所有以前的本地通知。但是它在iOS 9.0和更低版本中工作,但是在iOS 10中它会触发多个本地通知。因此,cancelAllLocalNotifications
似乎没有清除通知。
在iOS10中成功编译代码。
UIApplication.shared.cancelAllLocalNotifications()
发布于 2016-11-03 09:28:19
对于iOS 10,SWIFT3.0
cancelAllLocalNotifications
不推荐iOS 10。
@available(iOS,简介: 4.0,弃用: 10.0,消息:"Use UserNotifications Framework's -UNUserNotificationCenter removeAllPendingNotificationRequests")
您必须添加这个导入语句,
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"])
希望能帮上忙!
发布于 2016-11-08 19:44:57
For iOS 10,目标C
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllDeliveredNotifications];
[center removeAllPendingNotificationRequests];
Swift 4
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
如果要列出所有通知:
func listPendingNotifications() {
let notifCenter = UNUserNotificationCenter.current()
notifCenter.getPendingNotificationRequests(completionHandler: { requests in
for request in requests {
print(request)
}
})
}
发布于 2017-03-10 17:32:39
对于iOS 10,可以使用这种方式删除所有本地通知。我刚测试过了,很管用。
NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;
for (UILocalNotification *localNotification in arrayOfLocalNotifications) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification] ;
}
https://stackoverflow.com/questions/40397552
复制相似问题