Home > Enterprise >  didRegisterForRemoteNotificationsWithDeviceToken not called
didRegisterForRemoteNotificationsWithDeviceToken not called

Time:03-14

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: ".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()
            }
        }

CodePudding user response:

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

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