gpt4 book ai didi

ios - 在触发之前删除/避免本地通知

转载 作者:行者123 更新时间:2023-11-29 05:57:55 25 4
gpt4 key购买 nike

我正在安排一个每个工作日重复的本地通知(例如每周三的示例)

    let content = UNMutableNotificationContent()
content.title = notificationTitle
content.body = notificationBody

let currentCalendar = NSCalendar.current
let dateComponents = currentCalendar.dateComponents([.weekOfMonth], from: startDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: ViewController.kEveryWeekIdentifier, content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request) { (error) in

}

上面的代码工作正常,但我想在触发/传递之前删除特定的通知。
就这样可以去掉吗?或者至少有可能不在特定日期触发通知吗?

最佳答案

您可以通过两种方式删除通知:

1) 通过标识符

当您创建通知时,您设置了其标识符。您可以通过将该标识符传递给以下方法来删除通知:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arrayOfNotificationIds)

2) 按日期

另一个选项是整理所有计划的通知并按特定日期过滤它们,然后将计划在特定日期的通知传递给上述方法以将其删除。

您可以按如下方式获取所有预定通知:

UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: {requests -> () in }

您可能还会发现这很有帮助:从计划的通知中提取触发器(及其用于过滤目的的日期):

1)使用上述方法检索通知

2) 获取通知触发器。

let currentTrigger = currentRequest.trigger as! UNCalendarNotificationTrigger

3) 从触发器中获取日期组件

let dateComponents = currentTrigger.dateComponents

4)根据需要使用 dateComponents

过滤掉!

关于ios - 在触发之前删除/避免本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54966092/

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