gpt4 book ai didi

ios - cancelAllLocalNotifications 在 iOS10 中不起作用

转载 作者:IT王子 更新时间:2023-10-29 05:28:25 28 4
gpt4 key购买 nike

我想在添加新通知时从 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"])

希望对您有所帮助..!

关于ios - cancelAllLocalNotifications 在 iOS10 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40397552/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com