Custom input accessory view Notification

614 views Asked by At

At end of Advanced Notification Session 708 WWDC 2016. They talked about taking text input & action at same time.

Start at 24 min mark of WWDC session 708.

Where you take comment on party invite & accept or decline invite at same time. I tried to do that but am very unsuccessful.

class NotificationViewController: UIViewController, UNNotificationContentExtension {

var textField = UITextField(frame: CGRect(x: 0, y: 0, width: 100, height: 10))

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any required interface initialization here.
}

override var canBecomeFirstResponder: Bool {

    return true
}

override var inputAccessoryView: UIView? {

    let customView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))

    customView.backgroundColor = UIColor.red

    textField.placeholder = "Type Comment here"

    textField.textColor = UIColor.black

    customView.addSubview(textField)

    print(customView)

    return customView
}

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {

       if response.actionIdentifier == "comment" {

        becomeFirstResponder()

        textField.becomeFirstResponder()

        print("Its working")

        if let textResponse = response as? UNTextInputNotificationResponse {

            print(textResponse.userText)

            completion(UNNotificationContentExtensionResponseOption.doNotDismiss)
        }

        completion(UNNotificationContentExtensionResponseOption.doNotDismiss)
    }
}

}

1

There are 1 answers

0
kuhr On

Not sure exactly what your problem is, but I recently faced a similar task - and I've successfully implemented the solution from your referred WWDC talk.
My problem was that I didn't know how to dismiss the notification when using a custom inputAccessoryView. My solution was to save the completionHandler and then call it when a specific button in my custom inputAccessoryView is clicked.

First; create the function variable to store the completionHandler:

var savedCompletionBlock:(UNNotificationContentExtensionResponseOption) -> Void = { (UNNotificationContentExtensionResponseOption) in }

Then; in func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) save the completion block:

savedCompletionBlock = completion

And finally; call it wherever you need to (e.g. on a button click):

func confirmPressed() {
   savedCompletionBlock(.dismiss)
}

If this doesn't help you let me know :)