使用DatePicker安排通知(swift3)

时间:2017-07-17 14:18:48

标签: ios swift3 datepicker notifications ibaction

当用户使用datePicker操作选择要显示通知的日期和时间时,我希望显示用户通知。

 import UIKit
 import UserNotifications

 class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
       }

@IBAction func datePicker(_ sender: Any) {
}
@IBAction func disaper(_ sender: Any) {

    let c = UNMutableNotificationContent()
    c.title = "Lets Roll"
    c.subtitle  = "dx"
    c.body = "fyb"

    let t = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let r = UNNotificationRequest(identifier: "any", content: c, trigger: t)

    UNUserNotificationCenter.current().add(r, withCompletionHandler: nil)

}}

1 个答案:

答案 0 :(得分:0)

如果您要安排在确切日期发送通知,则需要使用UNCalendarNotificationTrigger代替UNTimeIntervalNotificationTrigger。您还应该为DatePicker设置一个插座,这样可以更轻松地设置通知触发器。

let triggerDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: datePicker.date )
let t = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)