UNTextInputNotificationAction not working on watch

Viewed 35

UNTextInputNotificationAction not working on watch when watchKitExtension install on app! When watchKitExtension don't show on watch - UNTextInputNotificationAction works correctly! When watchKitExtension show on watch - UNTextInputNotificationAction button pressed but nothing happens

What should I do so that when the watchKitExtension is installed, pressing the button opens the standard UNTextInputNotificationAction?

public func registerForNotifications() {
    UNUserNotificationCenter.current().delegate = self
    let answerToChatAction = UNTextInputNotificationAction(identifier: "answerToChat",
                                                           title: "Answer",
                                                           options: [],
                                                           textInputButtonTitle: "Send",
                                                           textInputPlaceholder: "Type here…")
    let messageCategory = UNNotificationCategory(identifier: "ChatMessage", actions: [ answerToChatAction], intentIdentifiers: [], options: [.allowInCarPlay])
    UNUserNotificationCenter.current().setNotificationCategories(Set([messageCategory]))
    let authOptions: UNAuthorizationOptions = [.alert, .sound, .badge]
    UNUserNotificationCenter.current().requestAuthorization(
        options: authOptions,
        completionHandler: { _, _ in })
    DispatchQueue.main.async {
        UIApplication.shared.registerForRemoteNotifications()
    }
}

handling :

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        switch response.actionIdentifier {
        case "answerToChat":
            answerOnPushInChat(response)
        default:
            print("Unknown action")
        }
        completionHandler()
    }

fileprivate func answerOnPushInChat(_ response: UNNotificationResponse) {
    guard let userText = (response as? UNTextInputNotificationResponse)?.userText else { return }
    postMessage(message: userText).run()
}
1 Answers

My bad. The problem was calling the completionHandler without waiting for my rest response.

Related