当用户使用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)
}}
答案 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)