How to schedule local notifications at slightly different times everyday in Swift?

I have this code that currently schedules a local notification to be fired a set amount of time before an event (e.g: 30 mins before golden hour begins). It works fine the first day and schedules the notification exactly 30 mins before the event time, however the next day, since golden hour has now changed by 2 mins due to sunset times, the notification is still being scheduled 30 mins before the time of the day before.

For example, if golden hour is at 17:52 today, notification is set for 30 mins before and therefore will correctly fire at 17:22. Tomorrow, golden hour is now 17:54, but the notification will still fire at 17:22, not 17:24. As you can see, after a week or 2, the notification times will be completely off.

This is my function that schedules a local notification, setting repeats in the trigger to true:

(I've added ... where extra code not relevant to the question is, just to make it easier to understand what's going on)
Code Block
func scheduleNotificationBefore(hour: Int, min: Int) {
userNotificationsCentre.delegate = self
let morningGoldenHourContent = UNMutableNotificationContent()
morningGoldenHourContent.title = "Morning Golden Hour soon"
...
morningGoldenHourContent.categoryIdentifier = "morningGoldenHour"
morningGoldenHourContent.sound = UNNotificationSound.default
let eveningGoldenHourContent = UNMutableNotificationContent()
eveningGoldenHourContent.title = "Evening Golden Hour soon"
...
eveningGoldenHourContent.categoryIdentifier = "eveningGoldenHour"
eveningGoldenHourContent.sound = UNNotificationSound.default
let latitudeDouble = PersistenceManager.retrieveLatitude()
let longitudeDouble = PersistenceManager.retrieveLongitude()
let sunlight = SunlightCalculator(latitude: latitudeDouble, longitude: longitudeDouble)
let sunriseGoldenHourStart = sunlight.calculate(.dawn, twilight: .custom(-4))
let sunsetGoldenHourStart = sunlight.calculate(.dusk, twilight: .custom(6))
let formattedSunriseGoldenHourStart = Calendar.current.dateComponents([.hour, .minute], from: sunriseGoldenHourStart ?? Date())
let formattedSunsetGoldenHourStart = Calendar.current.dateComponents([.hour, .minute], from: sunsetGoldenHourStart ?? Date())
var sunriseDateComponents = DateComponents()
sunriseDateComponents.hour = (formattedSunriseGoldenHourStart.hour ?? 0) - hour
sunriseDateComponents.minute = (formattedSunriseGoldenHourStart.minute ?? 0) - min
...
let sunriseTrigger = UNCalendarNotificationTrigger(dateMatching: sunriseDateComponents, repeats: true)
var sunsetDateComponents = DateComponents()
sunsetDateComponents.hour = (formattedSunsetGoldenHourStart.hour ?? 0) - hour
sunsetDateComponents.minute = (formattedSunsetGoldenHourStart.minute ?? 0) - min
...
let sunsetTrigger = UNCalendarNotificationTrigger(dateMatching: sunsetDateComponents, repeats: true)
let morningGoldenHourRequest = UNNotificationRequest(identifier: UUID().uuidString, content: morningGoldenHourContent, trigger: sunriseTrigger)
let eveningGoldenHourRequest = UNNotificationRequest(identifier: UUID().uuidString, content: eveningGoldenHourContent, trigger: sunsetTrigger)
userNotificationsCentre.removeAllPendingNotificationRequests()
userNotificationsCentre.removeAllDeliveredNotifications()
userNotificationsCentre.add(morningGoldenHourRequest, withCompletionHandler: nil)
userNotificationsCentre.add(eveningGoldenHourRequest, withCompletionHandler: nil)
}

And I call this function when the user hits on a button to select how many hours/mins before the golden hour times they would like to be notified:
Code Block
scheduleNotificationBefore(hour: 0, min: 30)

Is this the correct way to go about solving my issue? Am I missing something? Any help would be very useful. I'm happy to explain each line of code further, just ask!
The problem is that you can test only once a day, that makes debugging harder.

Very quick reading so far.
Have you checked, each day, the values of
let sunriseGoldenHourStart = sunlight.calculate(.dawn, twilight: .custom(-4))
let sunsetGoldenHourStart = sunlight.calculate(.dusk, twilight: .custom(6))

Why repeats true, do you really want it to be repeated once user has read ?
In anywise, I would try not to repeats false

Take care also, if formattedSunsetGoldenHourStart.minute is less than 30, you'll have an error.
Thank you for the quick response.

I've been testing this on my physical iPhone over the course of a few days so that's how I know it's not working (notification is triggered at wrong times).

You're right, I should probably check each day what the value of sunriseGoldenHourStart and sunsetGoldenHourStart. The problem is when I simply print the values out and run my code, this function won't be triggered until I tap on the 30mins before button to schedule the notification. This now prints out the correct sunriseGoldenHourStart value for today, but I will have the repeat the process the next day, again printing out the value for that current day and nothing later, so I'm now kinda stuck in this limbo. The point is that the user shouldn't need to be scheduling the notification 30mins before every single day. (I hope I've made any kinda of sense?)

According to the documentation, setting repeats to true means the notification will be repeated every day, rather than triggered once and stopping there, which is the functionality I want. I want the user to be notified every day 30 mins before golden hour starts.

And yes, I've removed some code in that function for clarity of the question, and some of that code basically checks if formattedSunsetGoldenHourStart.minute is less than 30 and does some simple logic to cover it. Thanks for pointing it out though!

I will try to debug some more with the values of sunrise and sunset GoldenHourStart and see what I'm getting. Thanks again for your response.
How to schedule local notifications at slightly different times everyday in Swift?
 
 
Q