使用 DatePicker 安排通知(swift3)
Use DatePicker to schedule notification (swift3)
我希望在用户使用 datePicker 操作 select 通知显示的日期和时间时显示我的用户通知。
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)
}}
如果您想安排在确切日期发送通知,您需要使用 UNCalendarNotificationTrigger
而不是 UNTimeIntervalNotificationTrigger
。您还应该为 DatePicker
设置一个 Outlet,这样可以更轻松地设置通知触发器。
let triggerDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: datePicker.date )
let t = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)
我希望在用户使用 datePicker 操作 select 通知显示的日期和时间时显示我的用户通知。
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)
}}
如果您想安排在确切日期发送通知,您需要使用 UNCalendarNotificationTrigger
而不是 UNTimeIntervalNotificationTrigger
。您还应该为 DatePicker
设置一个 Outlet,这样可以更轻松地设置通知触发器。
let triggerDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: datePicker.date )
let t = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)