ios - Swift:单击推送通知操作按钮时在文本字段中写入并处理它

标签 ios swift xcode unusernotificationcenter

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

我想做的事情:

如果我点击按钮,一个文本字段和键盘应该出现(从消息应用程序中知道)。在写了一些文本并提交/发送后,我想在 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" 的推送通知并且您用力点击它,将出现一个文本字段和键盘,然后您可以使用 print("Received text message:\(sendText)") 打印它。

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

关于ios - Swift:单击推送通知操作按钮时在文本字段中写入并处理它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51385439/

相关文章:

iPhone :How to get duration of video selected from library?

ios - 在 UITextField swift 3.0 中使用数字键盘以 dd-MM-yyyy 格式输入日期

iphone - Spotify/Rdio 底部工具栏 View Controller 转换

ios - 大型 SwiftUI 结构导致内存错误

ios - 无法在 iOS 消息扩展中使用 Realm?

ios - ld : file not found: -Obj-C error when building a new XCode project using embeded Cordova webview

swift - Xcode 9.2 编译速度太慢

ios - iOS SDK 7.0 + MKDirections + calculateDirectionsWithCompletionHandler

ios - 如果在 viewcontroller 中使用 UITabbar,如何设置 homeview controller

ios - 使用 NSnumberformatter 从计算得出的数字中删除数字