我使用的是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"
的推送通知,并且你在它上进行了强制触摸点击,就会出现一个文本字段和键盘,你可以用print("Received text message: (sendText)")
打印它。
别忘了从didFinishLaunchingWithOptions
呼叫showPushButtons()
,并为应用程序接收(远程(推送通知做好准备。