我想要安排一个本地通知每天在特定时间触发(即重复),但从明天开始。
例如:“从明天起,每天下午8点触发一次通知”
我参考了这个 Stack Overflow 网站上的问题作为指导,并且我认为我正在按照它所说的做,但是如果我在今天之前安排通知(例如在下午8点之前),仍会在当天收到通知:
func testDateNotification(){
let content = UNMutableNotificationContent()
content.title = "Test"
content.body = "This is a test"
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())
let userCalendar = Calendar.current
var components = userCalendar.dateComponents([.hour, .minute], from: tomorrow!)
components.hour = 20
components.minute = 00
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if ((error) != nil){
print("Error \(String(describing: error))")
}
}
}