如何解决如何在 Swift 中每天在稍微不同的时间安排本地通知?
我有这个代码,它当前安排本地通知在事件发生前的一定时间内触发(例如:黄金时段开始前 30 分钟)。它在第一天工作正常,并在活动时间前 30 分钟安排通知,但是第二天,由于黄金时段现在由于日落时间而改变了 2 分钟,通知仍然在活动时间前 30 分钟安排前一天。
例如,如果今天的黄金时段是 17:52,则通知会提前 30 分钟设置,因此会在 17:22 正确触发。明天的黄金时段现在是 17:54,但通知仍会在 17:22 而不是 17:24 触发。如您所见,一两周后,通知时间将完全关闭。
(我添加了...与问题无关的额外代码,只是为了更容易理解发生了什么)
feat_event <- function(x){
start <- dplyr::first(x)
end <- dplyr::last(x)
length <- difftime(end,start,units = "h")
output <- tibble(
start = unname(start),end = unname(end),length = unname(length)
)
output
}
feat_event(df$datetime)
# A tibble: 1 x 3
# start end length
# <dttm> <dttm> <drtn>
#1 2000-03-20 12:00:00 2000-03-21 17:00:00 29 hours
df %>%
as_tsibble(key = group,index = datetime) %>%
features(datetime,feat_event)
# A tibble: 2 x 4
# group start end length
# <chr> <dttm> <dttm> <drtn>
#1 A 2000-03-20 12:00:00 2000-03-21 02:00:00 14 hours
#2 B 2000-03-21 03:00:00 2000-03-21 17:00:00 14 hours
当用户点击按钮以选择他们希望在黄金时段之前多少小时/分钟收到通知时,我会调用此函数:
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,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)
}
这是解决我的问题的正确方法吗?我错过了什么吗?任何帮助都会非常有用。我很乐意进一步解释每一行代码,请问!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。