我正在使用 Xcode 9.4.1 (9F2000)
和 Swift
。
我在 AppDelegate
中有这段代码:
func showPushButtons(){
let replyAction = UNNotificationAction(
identifier: "reply.action",
title: "Reply to this message",
options: [])
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
if action == "reply.action"{
print("Reply button clicked")
completionHandler()
}
}
这使得以下内容:
收到推送通知时,将显示一个名为Reply to this message
的操作按钮。
目前,当点击按钮时,控制台打印Reply button clicked
。
我想做的事情:
如果我点击按钮,一个文本字段和键盘应该出现(从消息应用程序中知道)。在写了一些文本并提交/发送后,我想在 AppDelegate
中接收这条消息来处理它。
你知道怎么做吗?有什么提示吗?
最佳答案
此代码现在有效:
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"
的推送通知并且您用力点击它,将出现一个文本字段和键盘,然后您可以使用 print("Received text message:\(sendText)")
打印它。
不要忘记从 didFinishLaunchingWithOptions
调用 showPushButtons()
并准备应用以接收(远程)推送通知。
关于ios - Swift:单击推送通知操作按钮时在文本字段中写入并处理它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51385439/