【问题标题】:How to repeat local notification once fired at a particular time在特定时间触发后如何重复本地通知
【发布时间】:2018-05-08 03:16:24
【问题描述】:

我目前正在制作一个闹钟应用程序,我目前可能只是一个误解。我想在特定时间启动本地通知并按时间间隔重复。我得到了一些想法,这个 (https://makeapppie.com/2017/01/31/how-to-repeat-local-notifications/) 程序接近于我想要的,但我希望它在某个时间启动,然后每隔一段时间重复一次。

用户输入信息后,程序会吐出:

timeIntervalSeconds,用户希望收到通知的时间间隔(以秒为单位)

timeStart,通知何时开始

timeEnd,如果用户不停止通知,通知何时结束

任何建议将不胜感激。

谢谢!

【问题讨论】:

    标签: ios swift time uilocalnotification


    【解决方案1】:

    要在特定时间启动通知,请使用以下代码。

    let gregorian = Calendar(identifier: .gregorian)
                        let now = Date()
                        var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
    
                        // Change the time to 10:00:00 in your locale
                        components.hour = 10
                        components.minute = 0
                        components.second = 0
    
                        let date = gregorian.date(from: components)!
    
                        let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
                        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
    
                        let request = UNNotificationRequest(identifier: CommonViewController.Identifier, content: content, trigger: trigger)
    

    要在特定时间间隔内重复通知,请在触发器中使用以下代码。

     let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true)  // it repeats after every 2 minutes
    

    【讨论】:

      【解决方案2】:

      由于 Apple 仅提供有限的自动重复通知的间隔列表,您可以尝试在下一步使用自定义间隔: 当通知被触发时,您应该计算下一个通知的日期和时间,并将其安排在所需的时间

      【讨论】:

        猜你喜欢
        • 2018-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多