Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Firebase on iOS asks for the FCM token before APNs has given one

ErrorAPNS device token not set before retrieving FCM Token for Sender ID

Short answer

The Firebase Messaging SDK needs the APNs device token from iOS before it hands out an FCM token. If your code asks too early, if the APNs token never reaches Firebase, or if iOS never issues one, you get this message. Read the token in the delegate callback, map the APNs token yourself when swizzling is off, and check the push capability.

  • Applies to Xcode 26, firebase-ios-sdk 12, Swift 6, iOS 18
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log both tokens in one place. If the APNs line never prints, iOS has not issued a token and the problem is registration. If it prints after the FCM error, the problem is order.

    func application(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("APNs token received, \(deviceToken.count) bytes")
    }
    
    func application(_ application: UIApplication,
                     didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("APNs registration failed: \(error)")
    }
  2. 2Match what you see, then jump to its fix

    1. 1The message appears once at launch, and the APNs line prints after itThe FCM token is requested before the APNs token has arrivedFix
    2. 2The APNs line prints, but the Firebase message still appearsMethod swizzling is off, or the app is SwiftUI, so Firebase never sees the APNs tokenFix
    3. 3didFailToRegisterForRemoteNotificationsWithError prints, or no APNs line prints at alliOS never issues an APNs token because registration fails or is never requestedFix

What does the error look like?

  • APNS device token not set before retrieving FCM Token for Sender ID
  • Be sure to re-retrieve the FCM token once the APNS device token is set.

Cause 1 of 3

The FCM token is requested before the APNs token has arrived

1Check

Search the code for Messaging.messaging().token calls that run in didFinishLaunching or in a view's onAppear. The SDK logs this message when it starts a token fetch while it has no APNs token. Its release notes say that since 10.4.0 an APNs token must be provided on startup before an FCM token is retrieved.

grep -rn "Messaging.messaging().token" --include=*.swift .

2Fix

Do not poll for the token at launch. Set the messaging delegate after FirebaseApp.configure() and let the SDK call you when the token exists. It fires at each app start and whenever a new token is generated. Send the token to your server from that callback.

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    return true
}

extension AppDelegate: MessagingDelegate {
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        print("FCM token: \(fcmToken ?? "nil")")
    }
}

3Verify

Run on a real iPhone. The callback prints a token and the warning no longer appears after the APNs line.

# Xcode console: look for 'APNs token received' followed by 'FCM token:'

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

Method swizzling is off, or the app is SwiftUI, so Firebase never sees the APNs token

1Check

Firebase maps the APNs token to its own token by swizzling app delegate methods. It is switched off when Info.plist has FirebaseAppDelegateProxyEnabled set to NO, and Firebase's guide says SwiftUI apps must do the mapping by hand.

grep -rn "FirebaseAppDelegateProxyEnabled" --include=Info.plist .

2Fix

Hand the APNs token to Firebase yourself in the delegate method that receives it.

func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
}

3Verify

Launch again: the FCM token callback from the first cause now fires without the warning.

# Xcode console: the 'FCM token:' line prints once

Confirmed by firebase.google.com

Did this fix it?

Cause 3 of 3

iOS never issues an APNs token because registration fails or is never requested

1Check

Apple lists the reasons registration can fail: no network, APNs unreachable, or the app lacks the right code-signing entitlement. Open Signing & Capabilities and look for Push Notifications, which adds aps-environment. Also confirm the code calls registerForRemoteNotifications().

grep -rn "registerForRemoteNotifications" --include=*.swift .

2Fix

Add the Push Notifications capability to the app target and enable push for the App ID in your developer account. Call registerForRemoteNotifications() after the user answers the permission prompt.

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
    guard granted else { return }
    DispatchQueue.main.async {
        UIApplication.shared.registerForRemoteNotifications()
    }
}

3Verify

Rebuild, run on a device and confirm the 'APNs token received' line prints.

# Xcode console: 'APNs token received, 32 bytes'

Confirmed by developer.apple.com

Did this fix it?

How do I stop it coming back?

  • Treat the FCM token as something the SDK delivers to a delegate, not something you read once at launch.
  • Always implement didFailToRegisterForRemoteNotificationsWithError and log the error.
  • Test push code on a physical device, and keep the Push Notifications capability in the project, not only in a local setup.
  • If you turn swizzling off, write the apnsToken line in the same commit.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

The iOS push arrives from FCM but plays no sound

A custom sound name in the payload is ignored and nothing plays

iOS plays a sound for a remote notification only when the aps payload has a sound key and the app is allowed to play sounds.

3 causes, each with a fix

Mobile apps

InstanceID token() no longer works in a Swift Firebase app

Instance ID calls fail to compile after upgrading Firebase to 8.0.0 or later

Firebase removed the Instance ID dependency from Messaging in 8.0.0, so InstanceID.instanceID() calls no longer belong in a current app.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.