【发布时间】:2017-01-23 06:04:37
【问题描述】:
我想创建一个在应用运行时将显示在通知中心的 userNotification。 单击通知将终止应用程序。
需要使用哪个触发器? 如何实现此功能?
【问题讨论】:
标签: ios swift3 usernotifications unusernotificationcenter
我想创建一个在应用运行时将显示在通知中心的 userNotification。 单击通知将终止应用程序。
需要使用哪个触发器? 如何实现此功能?
【问题讨论】:
标签: ios swift3 usernotifications unusernotificationcenter
试试这个。
func scheduleNotification(at date: Date, body: String) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Dont Forget"
content.body = body
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
//UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
【讨论】: