0

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

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

This makes the following:

When receiving a push notification, an action button will be visible called Reply to this message.

Currently, when clicking on the button, console prints Reply button clicked.

What I want to do:

If I click on the button a textfield and the keyboard should appear (as known from messaging apps). After writing some text and submitting/sending I want to receive this message in AppDelegate to handle it.

Do you have any idea how to do this? Any hint?

David
  • 2,898
  • 3
  • 21
  • 57
  • 2
    Possible duplicate of [Adding text field in Remote Notification, iOS 8](https://stackoverflow.com/questions/25841227/adding-text-field-in-remote-notification-ios-8) – Kevinosaurio Jul 17 '18 at 15:50

2 Answers2

2

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.

David
  • 2,898
  • 3
  • 21
  • 57
0

Replace your replyAction with this one:

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

This should resolve your problem.

David
  • 2,898
  • 3
  • 21
  • 57