在我正在开发的应用程序中,有一个选项可以在为所述通知设置的实际时间之前触发通知x时间量 . 例如,我将提醒设置为10:00 . 但是在应用程序的本地设置中,我将通知设置为在设置的时间前10分钟触发 . 因此,在此示例中,通知将在9:50触发 .
现在,当我为个人通知设定时间时,我可以执行上述操作 . 但我想要做的是在为它设置的实际时间之前触发所有待处理的通知 .
这是我用来设置通知的功能:
func scheduleNotification(at date: Date, identifier: String, threadIdentifier: String, body: String) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, year: components.year, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "TestNotification"
content.body = body
content.threadIdentifier = threadIdentifier
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {
error in
if let error = error {
print("Error in delivering notification. Error: \(error.localizedDescription)")
}
}
}
日期来自日期选择器设置的日期 . 我尝试使用以下代码更改触发器属性:
UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
for request in requests {
request.trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10*60, repeats: false)
}
}
但是现在我得到一个错误,说“触发器”是一个只获取属性 .
1 回答
无法更改计划通知的开火时间,您可以删除所有这些并再次重新安排