Swift:从AppDelegate中的UNTextInputNotificationAction获取内容

时间:2018-07-17 16:28:32

标签: ios swift xcode

我正在使用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)"),我检查邮件不在actionrequestcontent中。

如您所见,我还检查了它是否在aps负载中,但不是。

1 个答案:

答案 0 :(得分:3)

此代码现在有效:

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"的推送通知,并在其中强行单击,将显示一个文本字段和键盘,您可以使用{{1}进行打印}。

别忘了从print("Received text message: \(sendText)")调用showPushButtons()并为接收(远程)推送通知准备应用程序。