问题描述
我想在应用程序收到操作响应时访问 AppDelegate.swift 中的应用程序数据.我试图使用
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {如果 response.actionIdentifier == "check" {//对应用程序的数据做一些事情}完成处理程序()}
方法,但我无法找到数据,因为我无法获得通知的标识符或其正文.有人可以帮我吗?非常感谢你们.
更多代码:
//设置通知内容让内容 = UNMutableNotificationContent()content.title = "预定任务"content.body = taskDescriptionTextField.text!内容.徽章 = 1content.sound = UNNotificationSound.default()content.categoryIdentifier = "alertCategory"//设置通知触发时间让日期 = datePicker.datelet dateCompenents = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)让触发器 = UNCalendarNotificationTrigger(dateMatching: dateCompenents, repeats: false)//添加请求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("原始标识符为:\(response.notification.request.identifier)")打印(原始正文是:\(response.notification.request.content.body)")打印(在通知中点击")}
基本上是这样,你得到一个 UNNotificationResponse
实例.该对象有两个属性.
var actionIdentifier: String
var notification: UNNotification
<-- 你需要使用这个.
也可以找到一个非常好的 UNUserNotification 框架教程 这里
如果您想知道用户在收到通知时选择了哪个操作(他们只是点击它吗?!他们是否刚刚关闭它?!)还是他们选择了 customAction?!)
//来自 Apple 文档:用户可以从中选择的操作标识符:
* UNNotificationDismissActionIdentifier 如果用户关闭了通知* UNNotificationDefaultActionIdentifier 如果用户从通知中打开了应用程序* 用于其他操作的已注册 UNNotificationAction 的标识符
它的意思是,你可以用来做这样的事情:
switch actionIdentifier {case UNNotificationDismissActionIdentifier://通知被用户拒绝//做点什么完成处理程序()case UNNotificationDefaultActionIdentifier://应用程序是从通知中打开的//做点什么完成处理程序()//做其他事情案例自定义操作:完成处理程序()默认:完成处理程序()}
要创建自定义操作,您必须:
- 在类别中创建一个动作
- 注册类别
有关更多信息,请参阅 WWDC 2016 预先通知
I want to access my application data in AppDelegate.swift whenever the app receives an action response. I was trying to use the
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "check" {
//do something to the app's data
}
completionHandler()
}
method, but I can't locate the data because I can't get the notification's identifier nor its body text. Could somebody help me on that? Thank you guys so much.
More Code:
//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)
Do:
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")
}
Basically what it is, is that you get a UNNotificationResponse
instance back. That object has two properties.
var actionIdentifier: String
var notification: UNNotification
<-- You need to use this one.
Also a very very good tutorial for UNUserNotification framework can be found here
You only use the actionIdenfier
if you want to find out which action did the user choose when they were presented a notification (Did they just just tap on it?! Did they just dismiss it?! Or did they select on a customAction?!)
// From Apple documentation: the action identifier that the user can chose from:
What it means is, you can use to do something like:
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()
}
To create custom actions you must:
- create an action inside a category
- register the category
For more information see this moment from WWDC 2016 Advance Notifications
这篇关于在 AppDelegate Swift 中获取本地通知的正文或标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!