我必须在我的应用中发出本地通知,该通知应在设置日期和时间时发出,该通知应在设置时间后1小时后30分钟之前发出。例如。如果通知日期和时间为2019-06-05 11:00 am,则本地通知应首先出现在上午10:30,然后出现在下午12:00。
这是我所做的代码,但它恰好与我设置的时间相同。
func schedule() {
let calendar = Calendar.current
let components = DateComponents(year: 2019, month: 06, day: 05, hour: 10, minute: 06) // Setting date here for local Notification
let date = calendar.date(from: components)
let comp2 = calendar.dateComponents([.year,.month,.day,.hour,.minute], from: date!)
let trigger = UNCalendarNotificationTrigger(dateMatching: comp2, repeats: false)
// let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Notification Demo"
content.subtitle = "Demo"
content.body = "Notification on specific date!!"
if #available(iOS 12.0, *) {
content.sound = UNNotificationSound.defaultCritical
} else {
content.sound = UNNotificationSound.default
}
let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if error != nil {
//handle error
} else {
//notification set up successfully
}
})
}
任何帮助将不胜感激。