每当应用程序收到操作响应时,我想在 AppDelegate.swift 中访问我的应用程序数据。我试图使用
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "check" {
//do something to the app's data
}
completionHandler()
}
方法,但我无法找到数据,因为我无法获取通知的标识符或其正文。有人可以帮我吗?非常感谢你们。
更多代码:
//Setting content of the notification
let content = UNMutableNotificationContent()
content.title = "Scheduled Task"
content.body = taskDescriptionTextField.text!
content.badge = 1
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "alertCategory"
//Setting time for notification trigger
let date = datePicker.date
let dateCompenents = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateCompenents, repeats: false)
//Adding Request
let request = UNNotificationRequest(identifier: taskDescriptionTextField.text!, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)