gpt4 book ai didi

ios - 如果重复设置为每天,则不会触发本地通知

转载 作者:行者123 更新时间:2023-11-30 12:48:45 26 4
gpt4 key购买 nike

我在尝试安排通知时遇到了一个非常奇怪的错误。用户提供想要接收本地通知的小时和分钟,并且可以选择是否希望每天、每周或每月安排通知。

但是,我遇到了一个非常奇怪的问题。这是我的代码:

 func enableLocalNotifications() {


notificationsSwitch.isOn = true
notificationsOn = true

//first cancel all current local notifications
UIApplication.shared.cancelAllLocalNotifications()

//create new one according to input

let calendar = Calendar(identifier: .gregorian)
var fireComponents=calendar.dateComponents([.day, .month, .year], from:NSDate() as Date)

fireComponents.hour = notificationHour
fireComponents.minute = notificationMinute
fireComponents.second = 00


fireComponents.calendar = calendar

let notification = UILocalNotification()
notification.timeZone = NSTimeZone.local
notification.alertBody = notificationDescription
notification.alertTitle = "Reminder"
notification.fireDate = fireComponents.date


notification.soundName = UILocalNotificationDefaultSoundName

switch notificationRepeat {
case "Daily":
notification.repeatInterval = NSCalendar.Unit.minute
case "Weekly":
notification.repeatInterval = NSCalendar.Unit.weekOfYear
case "Monthly":
notification.repeatInterval = NSCalendar.Unit.month
default:
break
}

UserDefaults.standard.setValue(notificationRepeat, forKey: "notificationRepeat")
UserDefaults.standard.setValue(notificationDescription, forKey: "notificationDescription")
UserDefaults.standard.setValue(notificationHour, forKey: "notificationHour")
UserDefaults.standard.setValue(notificationMinute, forKey: "notificationMinute")
UserDefaults.standard.setValue(true, forKey: "notificationSet")


}

}

有趣的是,例如,如果我想在 12:00 创建“每日”通知,而目前是 11:59,它不会在 12:00 触发通知,实际上它根本不发射任何东西。但是,我注意到,如果我将repeatInterval更改为NSCalendar.Unit.month,那么突然间,如果我安排12:00的通知,而现在是11:59,我真的会在12:00收到它。

我的错误有解决办法吗?

最佳答案

仔细检查您设置的notification.fireDate日期是否正确。有时,由于没有设置正确的时区,日期和时间会发生变化,并且不会在您期望的时间到来。

关于ios - 如果重复设置为每天,则不会触发本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41290818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com