cancelAllLocalNotifications在iOS10中不起作用

技术专家

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

代码在iOS10中成功编译。

UIApplication.shared.cancelAllLocalNotifications()
金刚狼

对于iOS 10,Swift 3.0

cancelAllLocalNotifications 自iOS 10起已弃用。

@available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]")
open func 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"])

希望能帮助到你..!!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章