Swift: 从 AppDelegate 中的 UNTextInputNotificationAction 获取内容
Swift: Get content from UNTextInputNotificationAction in AppDelegate
我正在使用 Xcode 9.4.1 (9F2000)
和 Swift
。
我在 AppDelegate
中有此代码:
func showPushButtons(){
let replyAction = UNTextInputNotificationAction(
identifier: "reply.action",
title: "Reply to message",
textInputButtonTitle: "Send",
textInputPlaceholder: "Input text here")
let pushNotificationButtons = UNNotificationCategory(
identifier: "allreply.action",
actions: [replyAction],
intentIdentifiers: [],
options: [])
UNUserNotificationCenter.current().setNotificationCategories([pushNotificationButtons])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let action = response.actionIdentifier
let request = response.notification.request
let content = response.notification.request.content.userInfo
print("\(action)\(request)\(content)")
if let aps = content["aps"] as? [String: AnyObject] {
dump(aps)
}
completionHandler()
}
它的作用:
收到推送通知时,将显示一个文本字段并出现键盘。我可以编写消息并提交。
我的问题是:如何在我的 AppDelegate
中获取此提交的消息来处理它(发送到我的服务器左右)?
使用 print("\(action)\(request)\(content)")
我检查了邮件不在 action
、request
或 content
.
中
如您所见,我还检查了它是否在 aps 负载中,但它不在。
此代码现在有效:
func showPushButtons(){
let replyAction = UNTextInputNotificationAction(
identifier: "reply.action",
title: "Reply on message",
textInputButtonTitle: "Send",
textInputPlaceholder: "Input text here")
let pushNotificationButtons = UNNotificationCategory(
identifier: "allreply.action",
actions: [replyAction],
intentIdentifiers: [],
options: [])
UNUserNotificationCenter.current().setNotificationCategories([pushNotificationButtons])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "reply.action" {
if let textResponse = response as? UNTextInputNotificationResponse {
let sendText = textResponse.userText
print("Received text message: \(sendText)")
}
}
completionHandler()
}
它的作用:如果您收到带有 "category":"allreply.action"
的推送通知并用力点击它,将出现一个文本字段和键盘,您可以使用 [= 打印它12=].
不要忘记从 didFinishLaunchingWithOptions
调用 showPushButtons()
并准备应用以接收(远程)推送通知。
我正在使用 Xcode 9.4.1 (9F2000)
和 Swift
。
我在 AppDelegate
中有此代码:
func showPushButtons(){
let replyAction = UNTextInputNotificationAction(
identifier: "reply.action",
title: "Reply to message",
textInputButtonTitle: "Send",
textInputPlaceholder: "Input text here")
let pushNotificationButtons = UNNotificationCategory(
identifier: "allreply.action",
actions: [replyAction],
intentIdentifiers: [],
options: [])
UNUserNotificationCenter.current().setNotificationCategories([pushNotificationButtons])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let action = response.actionIdentifier
let request = response.notification.request
let content = response.notification.request.content.userInfo
print("\(action)\(request)\(content)")
if let aps = content["aps"] as? [String: AnyObject] {
dump(aps)
}
completionHandler()
}
它的作用:
收到推送通知时,将显示一个文本字段并出现键盘。我可以编写消息并提交。
我的问题是:如何在我的 AppDelegate
中获取此提交的消息来处理它(发送到我的服务器左右)?
使用 print("\(action)\(request)\(content)")
我检查了邮件不在 action
、request
或 content
.
如您所见,我还检查了它是否在 aps 负载中,但它不在。
此代码现在有效:
func showPushButtons(){
let replyAction = UNTextInputNotificationAction(
identifier: "reply.action",
title: "Reply on message",
textInputButtonTitle: "Send",
textInputPlaceholder: "Input text here")
let pushNotificationButtons = UNNotificationCategory(
identifier: "allreply.action",
actions: [replyAction],
intentIdentifiers: [],
options: [])
UNUserNotificationCenter.current().setNotificationCategories([pushNotificationButtons])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "reply.action" {
if let textResponse = response as? UNTextInputNotificationResponse {
let sendText = textResponse.userText
print("Received text message: \(sendText)")
}
}
completionHandler()
}
它的作用:如果您收到带有 "category":"allreply.action"
的推送通知并用力点击它,将出现一个文本字段和键盘,您可以使用 [= 打印它12=].
不要忘记从 didFinishLaunchingWithOptions
调用 showPushButtons()
并准备应用以接收(远程)推送通知。