Dav*_*vid 0 xcode apple-push-notifications ios swift unusernotificationcenter
我正在使用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()
}
Run Code Online (Sandbox Code Playgroud)
它能做什么:
收到推送通知时,将显示一个文本字段并显示键盘。我可以写消息并提交。
我的问题是:我怎样才能得到这个提交的消息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()
}
Run Code Online (Sandbox Code Playgroud)
它的作用:如果您收到推送通知"category":"allreply.action"并用力点击它,将出现一个文本字段和键盘,您可以使用print("Received text message: \(sendText)").
不要忘记showPushButtons()从didFinishLaunchingWithOptions应用程序中调用并准备接收(远程)推送通知。
| 归档时间: |
|
| 查看次数: |
894 次 |
| 最近记录: |