設定某個時間觸發通知

從 UIDatePicker 元件選擇每天通知觸發的時間:
比方溫蒂在黑心的老闆虎克船長公司上班, 每天要工作到晚上11:30,所以彼得潘從 App 的 Date Picker 設定晚上 11:20,之後彼得潘將在每天晚上 11:20 收到通知,提醒溫蒂要下班了,要準備出門買點心接送她。

let date = datePicker.date
let calendar = Calendar.current
let components = calendar.dateComponents([ .hour, .minute],
from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching:
components, repeats: true)
let content = UNMutableNotificationContent()
content.body = "溫蒂要下班了,要準備出門買點心接送她"
let request = UNNotificationRequest(identifier:
"notification1", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request,
withCompletionHandler: nil)