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)
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:
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!
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!