我想创建一个在应用程序运行时将显示在通知中心的 userNotification。单击通知将终止应用程序。
需要使用哪个触发器?如何实现此功能?
我想创建一个在应用程序运行时将显示在通知中心的 userNotification。单击通知将终止应用程序。
需要使用哪个触发器?如何实现此功能?
尝试这个。
func scheduleNotification(at date: Date, body: String) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Dont Forget"
content.body = body
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
//UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}