我每天下午6点尝试在我的应用程序中触发本地通知。这是我正在使用的代码,但由于某些奇怪的原因,它并不想在特定时间触发。我做错了什么?
func notify() {
let calendar: NSCalendar = NSCalendar(calendarIdentifier: .gregorian)!
var dateFire=NSDate()
var fireComponents=calendar.components([.month, .day, .hour, .minute], from:NSDate() as Date)
fireComponents.hour = 18
fireComponents.minute = 0
dateFire = calendar.date(from: fireComponents)! as NSDate
let notification = UILocalNotification()
notification.alertBody = "Please take your tests for today! ❤️"
notification.fireDate = fireDate // this always works NSDate().addingTimeInterval(0.0001) as Date
notification.soundName = UILocalNotificationDefaultSoundName
notification.repeatInterval = NSCalendar.Unit.day
UIApplication.shared.scheduleLocalNotification(notification)
}
我也不想使用iOS 10中新的UserNotifications框架,因为我需要为iOS 9.0环境进行开发。