如何使用未来的开始日期(带时间)和结束日期(带时间)调度本地通知请求。有没有可能,因为我在框架中看不到任何变量
发布于 2021-09-03 04:36:06
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)
}您可以更改开始日期、结束日期和连续通知的间隔。
https://stackoverflow.com/questions/69026803
复制相似问题