Search code examples
delegateswatchkitunusernotificationcenterusernotifications

UNNotificationCenterDelegate not called when dismissing


I have an AppleWatch App, which receives remote Notifications. I get a callback on my notificationCenter(_:didRecieve response:...) when handling my custom action "a1" and the default action. However, this func isn't called for the custom dismiss action...

class ExtensionDelegate: NSObject, WKExtensionDelegate, UNUserNotificationCenterDelegate {

    func applicationDidFinishLaunching() {
        // Perform any final initialization of your application.
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound]) { (success, err) in

        }
        let a1 = UNNotificationAction(identifier: "a1", title: "Do Stuff", options: .foreground)
        let c1 = UNNotificationCategory(identifier: "c1", actions: [a1], intentIdentifiers: [], options: .customDismissAction)
        UNUserNotificationCenter.current().setNotificationCategories([c1])

            }
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            print("Action Identifier: \(response.actionIdentifier)"
            completionHandler()
    }
}

so my delegate seems to work and the categories too... what am I missing?


Solution

  • Fixed it: if you run your app on the Watch and dismiss it, the notificationCenter(_:didRecieve response:...) of your iOS App's UNUserNotificationCenterDelegate is called (in my case the AppDelegate)! Probably because when you dismiss it on the watch it is dismissed everywhere...

    Didn't find any resources tho.. if you did, please share the link :)