如何在特定时间触发后重复本地通知
How to repeat local notification once fired at a particular time
我目前正在制作一个闹钟应用程序,我目前陷入了可能只是一个误会。我想在特定时间启动本地通知并按时间间隔重复。我有一些想法,这个 (https://makeapppie.com/2017/01/31/how-to-repeat-local-notifications/) 程序接近我想要的,但我希望它在特定时间关闭,然后以一个间隔重复。
用户输入信息后,程序会吐出:
timeIntervalSeconds,用户想要通知的时间间隔(以秒为单位)
timeStart,通知开始的时间
timeEnd,如果用户不停止通知,通知将在何时结束
如有任何建议,我们将不胜感激。
谢谢!
由于 Apple 仅提供有限的自动重复通知间隔列表,您接下来可以尝试使用自定义间隔:
当一个通知被触发时,您应该计算下一个通知的日期和时间,并将其安排在所需的时间。
要在特定时间使用下面的代码启动通知。
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
我目前正在制作一个闹钟应用程序,我目前陷入了可能只是一个误会。我想在特定时间启动本地通知并按时间间隔重复。我有一些想法,这个 (https://makeapppie.com/2017/01/31/how-to-repeat-local-notifications/) 程序接近我想要的,但我希望它在特定时间关闭,然后以一个间隔重复。
用户输入信息后,程序会吐出:
timeIntervalSeconds,用户想要通知的时间间隔(以秒为单位)
timeStart,通知开始的时间
timeEnd,如果用户不停止通知,通知将在何时结束
如有任何建议,我们将不胜感激。
谢谢!
由于 Apple 仅提供有限的自动重复通知间隔列表,您接下来可以尝试使用自定义间隔: 当一个通知被触发时,您应该计算下一个通知的日期和时间,并将其安排在所需的时间。
要在特定时间使用下面的代码启动通知。
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