4

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
  • 169
  • 1
  • 2
  • 12
  • 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

2 Answers2

5

Adding for anybody else who feels intractibly stuck on this issue with a SwiftUI app; e.g. your code looks good, provisioning profiles are configured, etc etc etc, but still getting no response when calling UIApplication.shared.registerForRemoteNotifications() from didFinishLaunchingWithOptions in the AppDelegate...

what fixed it in my case was to add the following in my plist:

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
poliphilo
  • 51
  • 1
  • 2
  • 1
    This worked for me too. Guess in SwiftUI if you're using Firebase Messaging in the app, we need to disable swizzling and handle it manually. After disabling this to make sure FCM still works I had to manually set the device token to Firebase FCM. ```Messaging.messaging().apnsToken = deviceToken``` For more inforamtion visit this link. https://firebase.google.com/docs/cloud-messaging/ios/client#token-swizzle-disabled – Minon Weerasinghe May 07 '23 at 10:14
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
  • 1,579
  • 13
  • 19