【问题标题】:Swift: Get content from UNTextInputNotificationAction in AppDelegateSwift:从 AppDelegate 中的 UNTextInputNotificationAction 获取内容
【发布时间】:2018-07-17 16:28:32
【问题描述】:

我正在使用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 有效负载中,但不是。

【问题讨论】:

    标签: ios swift xcode apple-push-notifications unusernotificationcenter


    【解决方案1】:

    此代码现在可以使用:

    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() 并准备应用程序以接收(远程)推送通知。

    【讨论】:

      猜你喜欢
      • 2014-08-29
      • 2016-12-15
      • 2013-08-26
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      相关资源
      最近更新 更多