每当应用收到动作响应时,我想在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()
}
Run Code Online (Sandbox Code Playgroud)
方法,但我找不到数据,因为我无法获取通知的标识符或其正文.有人可以帮助我吗?非常感谢你们.
更多代码:
//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)
Run Code Online (Sandbox Code Playgroud)
做:
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")
}
Run Code Online (Sandbox Code Playgroud)
基本上它是什么,是你得到一个UNNotificationResponse实例.该对象有两个属性.
var actionIdentifier: Stringvar notification: UNNotification < - 你需要使用这个. 也是一个非常非常好的教程UNUserNotification框架,可以发现这里
你只想使用actionIdenfierif if你想知道用户在发出通知时选择了哪个动作(他们只是点击它了吗?他们只是解雇了它?!或者他们是否选择了customAction?!)
//来自Apple文档:用户可以从中选择的操作标识符:
Run Code Online (Sandbox Code Playgroud)* 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()
}
Run Code Online (Sandbox Code Playgroud)
要创建自定义操作,您必须:
有关更多信息,请参阅WWDC 2016 Advance Notifications的这一时刻
| 归档时间: |
|
| 查看次数: |
3384 次 |
| 最近记录: |