【发布时间】:2021-09-02 08:34:52
【问题描述】:
如何使用未来的开始日期(带时间)和结束日期(带时间)安排本地通知请求。是否有可能,因为我无法在框架中看到任何变量
【问题讨论】:
标签: swift unnotificationrequest
如何使用未来的开始日期(带时间)和结束日期(带时间)安排本地通知请求。是否有可能,因为我无法在框架中看到任何变量
【问题讨论】:
标签: swift unnotificationrequest
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.dateFormat = "dd MMM yyyy HH:mm"
guard let startDate = formatter.date(from: "03 Sep 2021 09:40")
, let endDate = formatter.date(from: "03 Sep 2021 10:40") else { return }
let intervalBetweenNotifications: Double = 5 * 60 // 5 minutes
for item in stride(from: startDate.timeIntervalSinceNow, to: endDate.timeIntervalSinceNow, by: intervalBetweenNotifications) {
let objectId = UUID().uuidString
let content = UNMutableNotificationContent()
content.title = "Title of the notification"
content.sound = .default
content.threadIdentifier = objectId
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: item, repeats: false)
let request = UNNotificationRequest(identifier: objectId, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
您可以更改开始、结束日期和连续通知之间的间隔。
【讨论】: