flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

[firebase_messaging]: iOS 15.x and APNS token has not been set yet.

Open petrnymsa opened this issue 10 months ago • 18 comments

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

petrnymsa avatar Mar 12 '25 13:03 petrnymsa

Hi @petrnymsa, please make sure you're using the latest version of firebase_messaging. Also, check out this comment. It might be helpful.

SelaseKay avatar Mar 12 '25 14:03 SelaseKay

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.

kirya355 avatar Mar 18 '25 06:03 kirya355

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

petrnymsa avatar Mar 18 '25 09:03 petrnymsa

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!

kirya355 avatar Mar 18 '25 14:03 kirya355

We are definitely calling getToken(). As stated in different issues we added call to getAPNSToken() before calling getToken().

Firebase is definitely initialized.

petrnymsa avatar Mar 18 '25 15:03 petrnymsa

I have the same error on a iPhone SE iOS 15.5

lsaudon avatar Mar 31 '25 12:03 lsaudon

Has this only been reproducible on a physical device? Have you been able to do it through simulator?

MichaelVerdon avatar Apr 03 '25 11:04 MichaelVerdon

It's reproductible on simulator

lsaudon avatar Apr 03 '25 12:04 lsaudon

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?

MichaelVerdon avatar Apr 04 '25 12:04 MichaelVerdon

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
  • FirebaseAppDelegateProxyEnabled is 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-catch to safely call getToken()
  • 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)

adarshwebcastle avatar Apr 07 '25 12:04 adarshwebcastle

If anyone is able to give a reliable minimal reproducible example it would help greatly and a fix can be investigated.

MichaelVerdon avatar Apr 10 '25 10:04 MichaelVerdon

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

Image

adarshwebcastle avatar Apr 10 '25 10:04 adarshwebcastle

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 avatar Apr 11 '25 14:04 MichaelVerdon

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 '';
    }
  }

jayadevpanthaplavil avatar Apr 15 '25 08:04 jayadevpanthaplavil

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.

pacifi5t avatar Apr 30 '25 23:04 pacifi5t

Having the same issue here.

StephanWalters avatar May 23 '25 00:05 StephanWalters

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

armdave avatar May 24 '25 03:05 armdave

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

shanelau avatar Jun 13 '25 01:06 shanelau

I fixed it by running the app once from Xcode (on a real device or simulator). After that, it worked fine in Android Studio.

nggiahao1999 avatar Jun 25 '25 05:06 nggiahao1999

Same issue on iphone 7 plus, ios 15.8.4. Working on ios 18.x

LeNgocHiep avatar Aug 15 '25 07:08 LeNgocHiep

Having the same issue here

HieuNghiemViet avatar Aug 28 '25 16:08 HieuNghiemViet

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.

MichaelVerdon avatar Aug 29 '25 07:08 MichaelVerdon

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
    }

VB10 avatar Sep 11 '25 22:09 VB10

Has anyone been able to reproduce this on any other iOS version?

MichaelVerdon avatar Sep 25 '25 04:09 MichaelVerdon

Has anyone been able to reproduce this on any other iOS version?

Yes. On IOS 17.4. I am getting the same error.

zaindarraj avatar Oct 04 '25 09:10 zaindarraj

any fix?

andikatp avatar Oct 09 '25 14:10 andikatp

any fix? or log more debug info

shanelau avatar Oct 10 '25 03:10 shanelau

It worked for me after I have removed the Push Notification Capability and added it again !

Amjed201 avatar Oct 14 '25 06:10 Amjed201

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!

google-oss-bot avatar Oct 22 '25 18:10 google-oss-bot

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.

google-oss-bot avatar Oct 30 '25 18:10 google-oss-bot