添加新通知时,我想从NotificationCenter删除所有以前的本地通知。但是它可以在iOS9.0及更低版本中使用,但在iOS 10中它会触发多个本地通知。因此,似乎cancelAllLocalNotifications无法清除通知。

代码在iOS10中成功编译。

UIApplication.shared.cancelAllLocalNotifications()

最佳答案

对于iOS 10,Swift 3.0

iOS 10不推荐使用cancelAllLocalNotifications



您将必须添加此导入语句,

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"])

希望能帮助到你..!!

10-08 08:07
查看更多