removeAllPendingNotificationRequests() 会永久取消本地时间通知吗?



我的 swift 应用程序中有一个时间通知,每天 8 点在本地发送推送通知。我想添加一个按钮,以便如果按下它,它只会取消当天的推送通知。我的问题是removeAllPendingNotificationRequests()做这项工作还是会永远取消时间通知?

它会取消您注册的所有待处理通知。在您的方案中,您可以将要注册的Notificationidenfifiers存储在字典中,其中键对应于类型为Date的每一天,值是类型为[String]identifiers数组。如果您想取消特定Date的所有Notification,您可以从上面的字典中获取该日期的String标识符数组,并像下面一样取消它们,下面是一个示例:

let notificationsDictionary = [Data: [String]]()
guard let notificationsForToday = notificationDictionary[Data()] else { return }
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: notificationsForToday)

最新更新