ios - Swift:从 AppDelegate 中的 UNTextInputNotificationAction 获取内容

标签 ios swift xcode apple-push-notifications 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()
}

它的作用:

当收到推送通知时,将显示一个文本字段并出现键盘。我可以编写消息并提交。

我的问题是:如何在我的 AppDelegate 中获取这个提交的消息来处理它(发送到我的服务器左右)?

使用 print("\(action)\(request)\(content)") 我检查消息不在 action, request,或内容

如您所见,我还检查了它是否在 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() 并准备应用以接收(远程)推送通知。

关于ios - Swift:从 AppDelegate 中的 UNTextInputNotificationAction 获取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51386248/

相关文章:

xcode - 如何进行击键以删除 Xcode 中的连续空格?

iphone - 如何在 iPhone 相机应用程序中使用各种效果或滤镜

ios - 添加新的自定义 UIButton 时,现有的自定义 UIButton 会变形

ios - SDwebimage EXC_BAD_ACCESS

ios - 获取核心数据最新 -id 并为新插入提供 -(+1)

ios - 是否可以从存储在iOS模拟器中的包中编译.ipa

ios - segue 向 View 添加导航

iOS Objective C - UITableView 性能问题

swift - 嵌套的 Swift 字典

iOS 图片上传到 AWS S3 Bucket 非常慢