【问题标题】:Start and End date in UNNotificationRequestUNNotificationRequest 中的开始和结束日期
【发布时间】:2021-09-02 08:34:52
【问题描述】:

如何使用未来的开始日期(带时间)和结束日期(带时间)安排本地通知请求。是否有可能,因为我无法在框架中看到任何变量

【问题讨论】:

    标签: swift unnotificationrequest


    【解决方案1】:
                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)
                }
    

    您可以更改开始、结束日期和连续通知之间的间隔。

    【讨论】:

    • 这将安排每 1 分钟通知一次,但在您的情况下,开始日期将是今天,我想更改未来的日期。
    猜你喜欢
    • 2012-08-19
    • 1970-01-01
    • 2022-01-23
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    相关资源
    最近更新 更多