如何在Datepicker中使用日期和时间创建本地通知?

时间:2020-05-18 15:05:20

标签: ios swift

我正在将文本字段用作日期选择器。我想通过在Textfield中获取数据来发送本地通知。我读了很多文章,但没有得到任何结果。你能帮我吗?

我的代码:

let content = UNMutableNotificationContent()
content.title = "Message Scheduler"
content.body = "It's time for send a message to the number \(number.text!)"
content.sound = UNNotificationSound.default
content.badge = 1

let datePicker = self.date.inputView as? UIDatePicker

let dateComponents = datePicker?.calendar.dateComponents(in: datePicker!.timeZone!, from: datePicker!.date)

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents!, repeats: false)

let userId = UUID().uuidString

let request = UNNotificationRequest(identifier: userId, content: content, trigger: trigger)

notificationCenter.add(request){ (error) in
    if let error = error{
        print(error.localizedDescription)
    }
}

0 个答案:

没有答案