'There is a way to set an notification to trigger in the first time in 8 hours and then in another time, like 4 in 4 hours in swift

I'm trying to make an algorithm that receives a time for the first dose of the medicine, and from there it triggers an alarm for X time from now, also selected by the user.

Example: First dose of the medicine was at 12:00 and I want to take it every 8 hours. Therefore, the first alarm must be set at 20:00 and the following, every 8 hours.

The problem is that if I set the first alarm to the desired time + 8, the alarm will not go off at 20, but yes. And if I only use it every 8 hours, I can't set the start time

func notifications(name: String, timeToTime: String, firstTime: String) {
    let center = UNUserNotificationCenter.current()
    getTimeForNotification(string: firstTime)

    let content = UNMutableNotificationContent()
    content.title = "It's time to take \(name)"
    content.body = "Remember that you must ingest this medicine from \(timeToTime)"
    content.sound = UNNotificationSound.default
    
    let timeInterval = handleToInt(time: timeToTime) // * 3600
    let trigger = UNTimeIntervalNotificationTrigger(
        timeInterval: timeInterval,
        repeats: true)
    
    let request = UNNotificationRequest(identifier: name, content: content, trigger: trigger)
    
    center.add(request)
}

first time is the variable that stores the time of the first dose, as a double, by the method getTimeForNotification() and timeInterval is the time, ex: 8 in 8, 12 in 12



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source