How to repeat local notification once fired at a particular time

↘锁芯ラ 提交于 2019-12-02 09:29:40

To launch notification on specific time use below code.

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)

And to repeat notifications on specific time interval use below code in trigger.

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

Since Apple provide only limited list of intervals for auto repeat of notifications you can try to do next to use custom intervals: When a notification got fired you should calculate date & time for next notification and just schedule it on desired time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!