Repeating notifications

Hello,

So I am trying to make a notification repeat every 5 hours. I figured out how to make it start at a specific time using datecomponents but I can’t figure out how to make it repeat or go off at another time.

 Button("Available: 9AM-3PM-7PM") {
                            let content = UNMutableNotificationContent()
                            content.title = "Feed \(dogsName)"
                            content.sound = UNNotificationSound.default
                            
                            var dateComponents = DateComponents()
                            dateComponents.hour = 9
                            dateComponents.minute = 00
                            
                            let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
                                
                            
                            let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
                            
                            UNUserNotificationCenter.current().add(request)
                                
                            }

Maybe something like this?

// Make a list to hold the notification times
var datComponentsList = [DataComponents]()

// Add the first dateComponents
var dateComponents1 = DateComponents()
dateComponents1.hour = 9
dateComponents1.minute = 00

// Append to the list of dateComponents
dateComponentsList.append(dateComponents1)

// Create the second notification time five hours later
var dateComponents2 = DateComponents()
dateComponents2.hour = 14
dateComponents2.minute = 00
// Append to the list of dateComponents
dateComponentsList.append(dateComponents2)

// Repeat for third and so on
// Create the second notification time five hours later
var dateComponents2 = DateComponents()
dateComponents3.hour = 19
dateComponents3.minute = 00
// Append to the list of dateComponents
dateComponentsList.append(dateComponents3)

// Loop through the list of dateComponents creating a notification for each one
for i..<dateComponentsList.count {
  let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents[i], repeats: true)
  let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger:    trigger)
                            
  UNUserNotificationCenter.current().add(request)
}

There’s a more elegant way to write this code as well, where you can create a loop that creates notification events up until a certain hour etc.

Also for testing purposes, maybe it’s better to test this five minutes apart. In that case, whatever time it is where you are, change the minutes versus the hour. You might also be able to change the date/ time of the simulator for this.

Using UNTimeIntervalNotificationTrigger as trigger simplifies it I think