【发布时间】:2023-03-21 12:29:01
【问题描述】:
我设置了一个本地通知,它应该在每天早上 8 点触发,但它确实会在早上 8 点触发 3 次,然后在上午 9 点和下午 12 点再次触发,之后它会停止并在下一次重复同样的事情天。
我检查了该函数是否被多次调用(不是),即使它被多次调用也不能解释在错误时间触发的原因。几乎所有我能想到的,重新实现了代码,但无济于事。
这就是我设置通知的方式:
func setUpMainLocalNotification() {
let notificationSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
if notificationSettings?.types == .None {
return
}
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let currentCalendar = NSCalendar.currentCalendar()
let date = NSDate()
let calendarComponents = calendar.components([.Year, .Month, .WeekOfMonth, .Hour, .Minute, .Second], fromDate: date)
calendarComponents.hour = 8
calendarComponents.minute = 0
calendarComponents.second = 0
currentCalendar.timeZone = NSTimeZone.defaultTimeZone()
let reminder = UILocalNotification()
reminder.timeZone = NSTimeZone.defaultTimeZone()
reminder.fireDate = currentCalendar.dateFromComponents(calendarComponents)
reminder.alertBody = "Notification"
reminder.category = "CATEGORY_ID"
reminder.repeatInterval = .Day
reminder.alertAction = "Reply"
reminder.soundName = "sound.aif"
UIApplication.sharedApplication().scheduleLocalNotification(reminder)
}
更新:
当我在这里阅读我自己的问题时,我注意到我在设置我希望我的通知触发的时间之后设置时区,所以我在设置时间之前将以下行移到它似乎工作,触发只有一次,早上 8 点。
currentCalendar.timeZone = NSTimeZone.defaultTimeZone()
【问题讨论】: