David
David

Reputation: 3318

Swift: Get content from UNTextInputNotificationAction in AppDelegate

I'm using Xcode 9.4.1 (9F2000) and Swift.

I have this code in 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()
}

What it does:

When receiving a push notification a textfield will presented and the keyboard appears. I'm able to write a message and submit.

My question is: How can I get this submitted message in my AppDelegate to handle it (send to my server or so)?

With print("\(action)\(request)\(content)") I checked the message isn't in action, request, or content.

As you can see I also checked if it's in the aps payload, but it isn't.

Upvotes: 1

Views: 1573

Answers (1)

David
David

Reputation: 3318

This code works now:

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()
}

What it does: If you're receiving a push notification with "category":"allreply.action" and you make a force touch click on it, a textfield and the keyboard will appear and you can you can print it with print("Received text message: \(sendText)").

Don't forget to call showPushButtons() from didFinishLaunchingWithOptions and prepare the app for receiving (remote) push notifications.

Upvotes: 11

Related Questions