Ios Swift:从AppDelegate中的UNTextInputNotificationAction获取内容
我使用的是Xcode 9.4.1 9F2000和Swift 我在AppDelegate中有以下代码: 它的作用是: 收到推送通知时,将显示文本字段并显示键盘。我能够写一条信息并提交 我的问题是:如何在AppDelegate中获取此已提交的消息,以便将其发送到服务器上 使用print\action\request\content,我检查了消息是否处于活动、请求或内容中 如您所见,我还检查了它是否在aps有效负载中,但它不在aps有效负载中。此代码现在起作用:Ios Swift:从AppDelegate中的UNTextInputNotificationAction获取内容,ios,swift,xcode,apple-push-notifications,unusernotificationcenter,Ios,Swift,Xcode,Apple Push Notifications,Unusernotificationcenter,我使用的是Xcode 9.4.1 9F2000和Swift 我在AppDelegate中有以下代码: 它的作用是: 收到推送通知时,将显示文本字段并显示键盘。我能够写一条信息并提交 我的问题是:如何在AppDelegate中获取此已提交的消息,以便将其发送到服务器上 使用print\action\request\content,我检查了消息是否处于活动、请求或内容中 如您所见,我还检查了它是否在aps有效负载中,但它不在aps有效负载中。此代码现在起作用: func showPushButton
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()
}
功能:如果您收到类别为:allreply.action的推送通知,并在其上进行强制触摸单击,则会出现文本字段和键盘,您可以使用printReceived文本消息:\sendText打印它
不要忘记从didFinishLaunchingWithOptions调用Show按钮,并准备应用程序以接收远程推送通知
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()
}