0

我有这段代码,目前安排本地通知在事件发生前的一段时间内触发(例如:黄金时段开始前 30 分钟)。它在第一天运行良好,并在活动时间前 30 分钟安排了通知,但是第二天,由于黄金时间现在由于日落时间已经改变了 2 分钟,通知仍然在活动时间前 30 分钟安排前一天。

例如,如果今天的黄金时段是 17:52,则通知设置为提前 30 分钟,因此将在 17:22 正确触发。明天,黄金时段现在是 17:54,但通知仍将在 17:22 触发,而不是 17:24。如您所见,一周或两周后,通知时间将完全关闭。

这是我安排本地通知的函数,将触发器中的重复设置为 true:

(我添加了......与问题无关的额外代码,只是为了更容易理解发生了什么)

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)) //this function calculates morning golden hour start time for the current date.
    let sunsetGoldenHourStart = sunlight.calculate(.dusk, twilight: .custom(6)) //this function calculates evening golden hour start time for the current date.
    
    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) //creating morning request
    let eveningGoldenHourRequest = UNNotificationRequest(identifier: UUID().uuidString, content: eveningGoldenHourContent, trigger: sunsetTrigger) //creating evening request
    
    userNotificationsCentre.removeAllPendingNotificationRequests()
    userNotificationsCentre.removeAllDeliveredNotifications()
    
    userNotificationsCentre.add(morningGoldenHourRequest, withCompletionHandler: nil)
    userNotificationsCentre.add(eveningGoldenHourRequest, withCompletionHandler: nil)
}

当用户点击一个按钮来选择他们希望在黄金时段之前多少小时/分钟时,我调用这个函数:

scheduleNotificationBefore(hour: 0, min: 30)

这是解决我的问题的正确方法吗?我错过了什么吗?任何帮助都会非常有用。我很乐意进一步解释每一行代码,请问!

4

0 回答 0