didRegisterForRemoteNotificationsWithDeviceToken method is not invoked in OSX App

Viewed 23

I'm developing a MacOS app and trying to registering for Push notifications using the below code.

    func initPushNotification() {
        
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
            guard granted else { return }
            
            DispatchQueue.main.async {
                NSApplication.shared.registerForRemoteNotifications()
            }
        }
    }

But the below methods are never called.

    func application(_ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("APNs token retrieved: \(token)")
    }
    
    func application(_ application: NSApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to register")
    }

Note: I have added Push notifications under capabilities and calling the initPushNotifications() in applicationDidFinishLaunching method When the above method is called, the OS shows a notification banner asking the user to grant permissions. Upon granting, NSApplication.shared.registerForRemoteNotifications() gets called but none of the callback delegate methods are invoked.

0 Answers
Related