在 AppDelegate 中获取本地通知的正文或标识符 Swift
Get local notification's body text or identifier in AppDelegate Swift
我想在应用程序收到操作响应时访问 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)
做:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("original identifier was : \(response.notification.request.identifier)")
print("original body was : \(response.notification.request.content.body)")
print("Tapped in notification")
}
基本上就是这样,您得到一个 UNNotificationResponse
实例。该对象有两个属性。
var actionIdentifier: String
var notification: UNNotification
<-- 你需要使用这个。
还可以找到关于 UNUserNotification 框架的非常非常好的教程 here
如果您想了解用户在收到通知时选择了哪个操作(他们只是点击它吗?!他们只是忽略它吗?!或者他们 select 在 customAction 上了吗?!)
// 来自 Apple 文档:用户可以选择的操作标识符:
* UNNotificationDismissActionIdentifier if the user dismissed the notification
* UNNotificationDefaultActionIdentifier if the user opened the application from the notification
* the identifier for a registered UNNotificationAction for other actions
意思是,您可以使用它来做类似的事情:
switch actionIdentifier {
case UNNotificationDismissActionIdentifier: // Notification was dismissed by user
// Do something
completionHandler()
case UNNotificationDefaultActionIdentifier: // App was opened from notification
// Do something
completionHandler()
// Do something else
case customAction:
completionHandler()
default:
completionHandler()
}
要创建自定义操作,您必须:
- 在类别中创建一个动作
- 注册类别
有关更多信息,请参阅 WWDC 2016 Advance Notifications
中的这一刻
我想在应用程序收到操作响应时访问 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)
做:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("original identifier was : \(response.notification.request.identifier)")
print("original body was : \(response.notification.request.content.body)")
print("Tapped in notification")
}
基本上就是这样,您得到一个 UNNotificationResponse
实例。该对象有两个属性。
var actionIdentifier: String
var notification: UNNotification
<-- 你需要使用这个。
还可以找到关于 UNUserNotification 框架的非常非常好的教程 here
如果您想了解用户在收到通知时选择了哪个操作(他们只是点击它吗?!他们只是忽略它吗?!或者他们 select 在 customAction 上了吗?!)
// 来自 Apple 文档:用户可以选择的操作标识符:
* UNNotificationDismissActionIdentifier if the user dismissed the notification * UNNotificationDefaultActionIdentifier if the user opened the application from the notification * the identifier for a registered UNNotificationAction for other actions
意思是,您可以使用它来做类似的事情:
switch actionIdentifier {
case UNNotificationDismissActionIdentifier: // Notification was dismissed by user
// Do something
completionHandler()
case UNNotificationDefaultActionIdentifier: // App was opened from notification
// Do something
completionHandler()
// Do something else
case customAction:
completionHandler()
default:
completionHandler()
}
要创建自定义操作,您必须:
- 在类别中创建一个动作
- 注册类别
有关更多信息,请参阅 WWDC 2016 Advance Notifications
中的这一刻