0

I have the following approach to get my UID for push notifications. Sadly, it's not being called. What I'm doing wrong? The registration for the push notifications is working. The error func is not being called. I'am getting crazy here.

extension AppDelegate {
    
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

        // 1. Convert device token to string
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }
        let token = tokenParts.joined()
        // 2. Print device token to use for PNs payloads
        print("Device Token: \(token)")

        }
    
    func application(
        _ application: UIApplication,
        didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Error registering notifications: (error)")
    }
}


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UIDevice.current.beginGeneratingDeviceOrientationNotifications()
        AppearanceConfigurator.configureNavigationBar()
        UIFont.overrideInitialize()
        
        
        requestNotificationAuthorization()
        print ("prepared for push notifications ...")
        
        return true
}

func getNotificationSettings() {
            UNUserNotificationCenter.current().getNotificationSettings { settings in
                print("User Notification settings: (settings)")
                guard settings.authorizationStatus == .authorized else { return }
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                    print ("registred for push notifications")
                }
            }
        }
        
        func requestNotificationAuthorization(){
            // Request for permissions
            UNUserNotificationCenter.current()
                .requestAuthorization(
                options: [.alert, .sound, .badge]) {
                    [weak self] granted, error in
                    //print("Notification granted: (granted)")
                    guard granted else { return }
                    self?.getNotificationSettings()
            }
        }

Simon Giesen
  • 77
  • 1
  • 1
  • 8
  • Are you enabled **Remote notifications** under TARGETS -> Signing $ Capabilities -> Background Modes – Sreekuttan Mar 14 '22 at 10:47
  • yes, I've added Remote Notifications under Background Modes – Simon Giesen Mar 14 '22 at 10:54
  • same issue [Here](https://stackoverflow.com/questions/55823643/didregisterforremotenotificationswithdevicetoken-is-not-getting-called-swift-5). Try any of this, hope it fixes the issue. – Sreekuttan Mar 14 '22 at 11:00
  • nothing helped so far, checked your link above – Simon Giesen Mar 14 '22 at 11:16
  • ok, I think I got the issue. You have a typo, missing a _ ,correct the method to **func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)** – Sreekuttan Mar 14 '22 at 11:34

1 Answers1

0

Change the didRegisterForRemoteNotificationsWithDeviceToken method. You are using the wrong method name.

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("Successfully registered for notifications!")
}
Sreekuttan
  • 748
  • 7
  • 14