您好我正在尝试实施日期/时间通知,但日期是 2 小时。
选择日期时的代码
func SelectTime(sender: UIView){
formatter.timeZone = TimeZone.autoupdatingCurrent
let timePicker = ActionSheetDatePicker(title: "Time:", datePickerMode: UIDatePickerMode.time, selectedDate: userDate, doneBlock: {
picker, userDateWithTime, index in
self.formatter.timeZone = TimeZone.autoupdatingCurrent
self.formatter.dateFormat = "yyyy MMMM dd, HH:mm"
self.dateSelected.text = self.formatter.string(for: userDateWithTime)
//print("User date picked \(self.formatter.string(from: userDateWithTime as! Date))")
return
创建通知的代码
let uuid = UUID().uuidString
let notification = UNMutableNotificationContent()
notification.title = "Plus - Todo"
//notification.subtitle
notification.body = taskText.text!
let calendar = Calendar.current
let dateComponents = calendar.dateComponents(in: .current, from: userDateWithTime)
//let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: correctDate!)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
//let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: uuid, content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)