[firebase_messaging]: iOS 15.x and APNS token has not been set yet.
Is there an existing issue for this?
- [x] I have searched the existing issues.
Which plugins are affected?
Messaging
Which platforms are affected?
iOS
Description
I've read closed issues but none of them answered what is solution / workaround. Especially quoting:
https://github.com/firebase/flutterfire/issues/11548#issuecomment-1735474066
For me FirebaseMessaging.instance.getAPNSToken(); always returns null and I am using a physical device. Is there a fix for that?
On iOS 18.x real device everything works. On iOS 15.6 real device I am getting
APNS token has not been set yet. Please ensure the APNS token is available by calling `getAPNSToken()`
even after calling getAPNSToken(). This method returns immediately with null.
Reproducing the issue
Tried to call _fcmInstance.getToken() on iOS 15.6
Firebase Core version
3.10.1
Flutter Version
3.27.3
Relevant Log Output
Flutter dependencies
Expand Flutter dependencies snippet
Replace this line with the contents of your `flutter pub deps -- --style=compact`.
Additional context and comments
No response
Hi @petrnymsa, please make sure you're using the latest version of firebase_messaging. Also, check out this comment. It might be helpful.
Hi @petrnymsa! Please make sure your aps-environment is correctly applied in your build settings. If everything is configured properly, you should see aps-environment: production in the your TestFlight last version -> Build Metadata -> Bottom section.
Hi @petrnymsa! Please make sure your aps-environment is correctly applied in your build settings. If everything is configured properly, you should see aps-environment: production in the your TestFlight last version -> Build Metadata -> Bottom section.
I've checked metadata in TestFlight and we have production aps-environment set
Hi @petrnymsa! Please make sure your aps-environment is correctly applied in your build settings. If everything is configured properly, you should see aps-environment: production in the your TestFlight last version -> Build Metadata -> Bottom section.
I've checked metadata in TestFlight and we have production aps-environment set
Thanks for confirming the aps-environment setting. Since the configuration seems correct, could you try using FirebaseMessaging.instance.getToken() instead of getAPNSToken()? This might help resolve the issue.
Also, please double-check that you’ve properly initialized Firebase in your app with:
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
If the issue persists, please try reproducing the issue using the latest commit from the firebase_messaging example in the official FlutterFire Firebase Messaging Example
Let me know if this helps or if you encounter any further issues!
We are definitely calling getToken(). As stated in different issues we added call to getAPNSToken() before calling getToken().
Firebase is definitely initialized.
I have the same error on a iPhone SE iOS 15.5
Has this only been reproducible on a physical device? Have you been able to do it through simulator?
It's reproductible on simulator
I have not been able to reproduce this on our example app, you can check our example app here to compare implementations: here. A few more things, can you confirm:
- Device has been authorised for notifications,
- Went on to XCode and enabled push notifications to signing and abilities?
- Ensured initializeApp finishes execution before making a call to get APNS token? And if possible could you provide a snippet showing your usage of getAPNSToken() so I can have a better idea of what is going wrong?
I was also facing the same issue despite trying several resolutions from GitHub and other forums.
Tested Devices:
- iOS 18.3.1 & iOS 18.3.2 → Issue persists. If we wait long enough, it eventually generates the APNS token.
Everything is configured correctly:
- Push Notification capabilities are enabled
-
FirebaseAppDelegateProxyEnabledis not set to false - AppDelegate doesn't interfere with Firebase setup
Temporary Workaround:
Don't block functionality while waiting for the token. Instead:
- Use
try-catchto safely callgetToken() - Allow users to proceed even if token is not available initially
- Listen for token updates later and update it to your server when available
- macOS: Sequoia 15.4 (24E248)
- Xcode: 16.3 (16E140)
If anyone is able to give a reliable minimal reproducible example it would help greatly and a fix can be investigated.
If anyone is able to give a reliable minimal reproducible example it would help greatly and a fix can be investigated.
final _firebaseMessaging = FirebaseMessaging.instance;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(MyApp());
}
Future<void> checkForNotificationPermission() async {
final settings = await _firebaseMessaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
}
class MyApp extends StatefulWidget {
...
await checkForNotificationPermission();
final token = await _firebaseMessaging.getToken();
}
[firebase_messaging/apns-token-not-set] APNS token has not been set yet. Please ensure the APNS token is available by calling
getAPNSToken()
After the token fetch function is executed, you may notice a delay on iOS devices (especially on versions like iOS 18.3.x). The token may or may not be returned immediately and show the above error
firebase_core: ^3.13.0 firebase_messaging: ^15.2.5
Push Notification capabilities are enabled FirebaseAppDelegateProxyEnabled is not set to false AppDelegate doesn't interfere with Firebase setup
To try something out, can I get you to attach getAPNSToken() to a button and print it to console to see if that works for you? Could help us determine if it is a thread related issue im thinking or where you try to get token. Im suspecting it may not have the time to resolve?
I tried calling getAPNSToken() via a button press and logged the result. Unfortunately, it still returns null:
[log] ApnsToken: null
[log] Error getting Firebase device token: [firebase_messaging/apns-token-not-set] APNS token has not been set yet. Please ensure the APNS token is available by calling `getAPNSToken()`.
TextButton(onPressed: (){
viewModel.getDeviceToken();
}, child: const Text("Generate", style: TextStyle(color: Colors.black),))
Future<String> getDeviceToken() async {
try {
final apnsToken = await messaging.getAPNSToken();
log('ApnsToken: $apnsToken');
final token = await messaging.getToken();
if (token != null && token.isNotEmpty) {
log('FirebaseToken: $token');
return token;
} else {
log('FirebaseToken is null or empty');
return '';
}
} catch (e, stack) {
log('Error getting Firebase device token: $e');
return '';
}
}
Hi everyone! I've encountered the exact same problem. My collegue uses an iPhone with iOS 18 and everything works perfectly fine, but my iPhone 7 with iOS 15 has this issue.
Flutter version
3.29.3
Firebase plugins versions
firebase_core - 3.12.1
firebase_messaging - 15.2.4
I've tried upgrading Firebase plugins to their latest versions (3.13.0 for firebase_core and 15.2.5 firebase_messaging), but it didn't solve the issue.
To try something out, can I get you to attach getAPNSToken() to a button and print it to console to see if that works for you? Could help us determine if it is a thread related issue im thinking or where you try to get token. Im suspecting it may not have the time to resolve?
@MichaelVerdon I've added calling getAPNSToken every 3 seconds when I need to get the FCM token to my app. The results are printed to the debug console. After 5 minutes, the result of calling getAPNSToken is still null.
Having the same issue here.
Running into the same issue.
[log] Error getting Firebase device token: [firebase_messaging/apns-token-not-set] APNS token has not been set yet. Please ensure the APNS token is available by calling `getAPNSToken()`.
My code is similar to this
final apnsToken = await messaging.getAPNSToken();
log('ApnsToken: $apnsToken');
final token = await messaging.getToken();
Also tried setting a delay between requesting permission and getAPNSToken(), but no luck.
iPhone: iphone 16 pro ios: 18.4.1 firebase_core: 3.13.0 firebase_messaging: 15.2.5
Same issue on macOS.
> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.32.3, on macOS 15.5 24F74 darwin-arm64)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 16.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.3)
[✓] VS Code (version 1.100.3)
[✓] Connected device (3 available)
[✓] Network resources
I fixed it by running the app once from Xcode (on a real device or simulator). After that, it worked fine in Android Studio.
Same issue on iphone 7 plus, ios 15.8.4. Working on ios 18.x
Having the same issue here
This is likely just an iOS 15 bug which I doubt we can address here. Regarding what you said @adarshwebcastle I have been completely unable to reproduce in iOS 18, the APNS token reliably prints out first try no matter what. Whilst I do not encourage using older versions, if you really need this to work on iOS 15 you could downgrade versions worst case scenario.
It works correctly once I added setApnsToken from App.delegate. Without any crashes, I am able to retrieve the APN token normally.
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().setAPNSToken(deviceToken, type: .unknown)
// Insider.registerDeviceToken
}
Has anyone been able to reproduce this on any other iOS version?
Has anyone been able to reproduce this on any other iOS version?
Yes. On IOS 17.4. I am getting the same error.
any fix?
any fix? or log more debug info
It worked for me after I have removed the Push Notification Capability and added it again !
Hey @petrnymsa. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
Since there haven't been any recent updates here, I am going to close this issue.
@petrnymsa if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.