Swift: 点击推送通知操作按钮时写入文本框并处理

Swift: Write in textfield when clicking on push notification action button and handle it

我正在使用 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

我想做的事情:

如果我点击按钮,一个文本字段和键盘应该出现(从消息传递应用程序中知道)。在写了一些文字和 submitting/sending 之后,我想在 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" 的推送通知并用力点击它,将出现一个文本字段和键盘,您可以使用 [= 打印它12=].

不要忘记从 didFinishLaunchingWithOptions 调用 showPushButtons() 并准备应用以接收(远程)推送通知。

用这个替换你的 replyAction

let replyAction = UNTextInputNotificationAction(identifier: "reply.action", title: "message", options: [], textInputButtonTitle: "Send", textInputPlaceholder: "type something …")

这应该可以解决您的问题。