我通过仅设置小时、分钟和秒来安排每日重复通知:
func addNotification(fireDate: Date)
{
let notificationCenter = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Some title"
content.body = "Some message"
content.sound = .default
content.categoryIdentifier = "MyCategory"
let calendar = Calendar(identifier: .gregorian)
let triggerDate = calendar.dateComponents([.hour, .minute, .second], from: fireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)
let requestID = UUID().uuidString
let notificationRequest = UNNotificationRequest(identifier: requestID, content: content, trigger: trigger)
notificationCenter.add(notificationRequest) { error in
if let error = error
{
print(error.localizedDescription)
}
}
}
现在我希望能够取消当天的所有通知,这样我就可以从明天开始收到通知。我尝试继承 UNCalendarNotificationTrigger 以更改 nextTriggerDate,但使用我的自定义触发器类添加通知时出错。找不到实现此目的的方法,如果我在某个时间取消预定的通知,我将不再收到该通知。任何想法表示赞赏。