Wednesday, September 10, 2025
HomeiOS Developmentios - How can I retrieve the APNs earlier than the FCM...

ios – How can I retrieve the APNs earlier than the FCM token?


Earlier than encountering the error, I used to be utilizing Xcode which had Firebase put in. At round 12pm right this moment, I started encountering an error that regarded like this

APNS gadget token not set earlier than retrieving FCM Token for Sender ID 'XXXX'. Make sure you re-retrieve the FCM token as soon as the APNS gadget token is about.

Previous to 12pm, I didn’t encounter the error in any respect

After tinkering with it for 9 hours, I attempted shifting strategies round, regarded up many web sites to seek out the precise error, debugging, and even went to YouTube of all locations, however cannot work out the place the error is coming from.

If it helps, I’m at the moment utilizing Xcode 16 with Firebase model 10.27.

Here is the code for anybody who thinks they’ll discover the reply

That is in my AppDelegate from fixed debugging

For further context:

  • I’ve the app working on my iPhone 15 Professional Max and was working properly earlier than the error
  • I’ve Background Modes enabled (Background fetch, processing, distant notifications)
  • In my Firebase Console, I’ve the APN key in my Cloud Messaging part
  • I’ve added the app to my Firebase server
  • I’ve the Google Data.plist in my code
  • I’ve the app registred for App Attest (AppCheck) and DeviceCheck
    // This methodology will get referred to as when the app efficiently registers for distant notifications and receives a tool token from APNs
    func software(_ software: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Knowledge) {
        Messaging.messaging().apnsToken = deviceToken
            print("APNs Machine Token: (deviceToken)")

           // Fetch the FCM registration token (this token is used to ship push notifications by Firebase)
           Messaging.messaging().token { token, error in
               if let error = error {
                   print("Error fetching FCM registration token: (error)")
               } else if let token = token {
                   print("FCM registration token: (token)")
               }
           }
       }

       // This methodology known as when the app finishes launching
    func software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Initialize Firebase
        FirebaseApp.configure()
        
        // Log app open occasion (for Firebase Analytics)
        FirebaseAnalytics.Analytics.logEvent(AnalyticsEventAppOpen, parameters: nil)
        
        // Set the delegate for Firebase Messaging
        Messaging.messaging().delegate = self
        
        // Register for distant notifications
        software.registerForRemoteNotifications()
        
        // Request notification authorization from the person
        UNUserNotificationCenter.present().requestAuthorization(choices: [.alert, .sound, .badge]) { granted, error in
            if granted {
                DispatchQueue.predominant.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
        }
        
        // Set UNUserNotificationCenter delegate
        UNUserNotificationCenter.present().delegate = self
        
        return true
    }
    
    
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        if let fcm = Messaging.messaging().fcmToken {
            print("fcm", fcm)
        }
    }

    func userNotificationCenter(_ heart: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        handleDailyReportTrigger()
        completionHandler([.banner, .sound, .badge])
    }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments