flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

πŸ› [FirebaseMessaging] Unable to get the FCM token in release mode on iOS

Open Chia-Wei-Du opened this issue 3 years ago β€’ 9 comments

Bug report

I used the latest version of firebase_core 2.21.0 and firebase_messaging 14.7.3, I can normally get the FCM token on iOS by directly run the flutter app in debug mode. But when I build the release mode to generate the IPA file for iOS , then put the IPA file to the real mobile device and install to device, I can not get the FCM token(No Response).

By the way, everything is fine on Android.

Steps to reproduce

Steps to reproduce the behavior:

  1. Go to pubspec.yaml and set the two packages below dependencies: firebase_core: ^2.21.0 firebase_messaging: ^14.7.3
  2. Click Pub get
  3. Switch to the Terminal and type: flutter build ipa -t lib/main.dart
  4. Get the IPA file from build/ios/ipa/ and put the file to the real mobile device(I use "Sideloadly").

Sample project

Using the Firebase Messaging Example example


Additional context

I found a workaround method from here by using the old version below firebase_core: ^1.24.0 firebase_messaging: ^12.0.3 firebase_core_platform_interface: 4.5.1 And it worked! I can get the FCM token in the release mode on iOS.

Then I try to use below firebase_core: ^2.0.0 firebase_messaging: ^14.0.0 It failed. So I think this problem may arise from this version.


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
[βœ“] Flutter (Channel stable, 3.13.9, on macOS 14.1 23B74 darwin-arm64, locale zh-Hant-TW)
[βœ“] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[βœ“] Xcode - develop for iOS and macOS (Xcode 15.0.1)
[βœ“] Chrome - develop for the web
[βœ“] Android Studio (version 2022.3)
[βœ“] VS Code (version 1.84.1)
[βœ“] Connected device (4 available)
[βœ“] Network resources

β€’ No issues found!

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand
Resolving dependencies... 
Got dependencies.
Dart SDK 3.1.5
Flutter SDK 3.13.9
firebase_messaging_example 0.0.0

dependencies:
- firebase_core 2.21.0 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_messaging 14.7.3 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math web sky_engine]
- flutter_local_notifications 14.1.4 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone]
- http 0.13.6 [async http_parser meta]

dependency overrides:
- plugin_platform_interface 2.1.6 [meta]

transitive dependencies:
- _flutterfire_internals 1.3.11 [collection firebase_core firebase_core_platform_interface flutter meta]
- args 2.4.2
- async 2.11.0 [collection meta]
- boolean_selector 2.1.1 [source_span string_scanner]
- characters 1.3.0
- clock 1.1.1
- collection 1.17.2
- dbus 0.7.8 [args ffi meta xml]
- fake_async 1.3.1 [clock collection]
- ffi 2.1.0
- firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.8.1 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_messaging_platform_interface 4.5.12 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.5.12 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- flutter_local_notifications_linux 4.0.0+1 [dbus ffi flutter flutter_local_notifications_platform_interface path xdg_directories]
- flutter_local_notifications_platform_interface 7.0.0+1 [flutter plugin_platform_interface]
- flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math async boolean_selector characters collection material_color_utilities meta source_span stream_channel string_scanner term_glyph web]
- flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math web]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- js 0.6.7 [meta]
- matcher 0.12.16 [async meta stack_trace term_glyph test_api]
- material_color_utilities 0.5.0 [collection]
- meta 1.9.1
- path 1.8.3
- petitparser 5.4.0 [meta]
- sky_engine 0.0.99
- source_span 1.10.0 [collection path term_glyph]
- stack_trace 1.11.0 [path]
- stream_channel 2.1.1 [async]
- string_scanner 1.2.0 [source_span]
- term_glyph 1.2.1
- test_api 0.6.0 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph]
- timezone 0.9.2 [path]
- typed_data 1.3.2 [collection]
- vector_math 2.1.4
- web 0.1.4-beta
- xdg_directories 1.0.3 [meta path]
- xml 6.3.0 [collection meta petitparser]


Chia-Wei-Du avatar Nov 09 '23 08:11 Chia-Wei-Du

I faced the same issue too

Ellynnn avatar Nov 09 '23 10:11 Ellynnn

@Chia-Wei-Du Can you confirm that you have configured / setup based on the documentation ?

darshankawar avatar Nov 09 '23 11:11 darshankawar

@darshankawar Yes, I have configured base on the documentation. ζˆͺεœ– 2023-11-10 上午9 23 24 ζˆͺεœ– 2023-11-10 上午9 23 51

As I said above, I can get the fcm token and receive the notification in the debug mode by running my project directly. But it failed in the release mode, just like using TestFlight. The fcm token is never received.

The point is, everything works fine in older versions: firebase_core: ^1.24.0 firebase_messaging: ^12.0.3 firebase_core_platform_interface: 4.5.1

Chia-Wei-Du avatar Nov 10 '23 01:11 Chia-Wei-Du

Hi @Chia-Wei-Du, does the method hang or returns an empty response? Do you have some crash monitoring setup that can catch any errors that may be occurring?

danagbemava-nc avatar Nov 10 '23 12:11 danagbemava-nc

@danagbemava-nc No. The method FirebaseMessaging.instance.getToken(); is no response.

But I test the FCM Messaging example below, hope this helps you a bit.

ζˆͺεœ– 2023-11-10 ζ™šδΈŠ9 11 16

Please see the token_monitor.dart ζˆͺεœ– 2023-11-10 ζ™šδΈŠ9 24 38

The example app start with the CircularProgressIndicator. If get the FCM token successfully using FirebaseMessaging.instance.getToken();, it will update the token by setState(() { _token = token; });. You can check the video below. FirebaseMessaging.instance.getToken(); is no response, so the CircularProgressIndicator keep running. No Response Video

If using the order version below, it get the FCM token successfully and it will update the toke on the screen. ζˆͺεœ– 2023-11-10 ζ™šδΈŠ9 13 10

Normal Video

Chia-Wei-Du avatar Nov 10 '23 14:11 Chia-Wei-Du

@darshankawar @danagbemava-nc

After few days of research from #10656 , #9878 , #10625 , #10742 , #11590, I figure out that it should be get the apns token successfully before getting the fcm token. Even if I successfully obtained the fcm token using the old version, I still cannot receive push notifications without obtaining the apns token. But even after I read all these issues, I still don't know how to get the apns key correctly in release mode on real device.

So I reorganized the question below:

Testing with run the both release and debug version by xcode, I can get the apns token and fcm token successfully. But If I build the release ipa and install by Testflight/Sideloadly, it will return the null apns token.

I saw someone delay few seconds and try again then get the apns token successfully. But for me, it not worked. It still return the null apns token.

And I also saw someone open the issue #11459 in ios sdk. I don't know if this issue is related to mine.

Here is my sample code.

FirebaseMessaging messaging = FirebaseMessaging.instance;
    NotificationSettings settings = await messaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );
    print('User granted permission: ${settings.authorizationStatus}');

    if (Platform.isIOS) {
      String? apnsToken;
      try {
        apnsToken = await messaging.getAPNSToken();
        if (apnsToken == null) {
          await Future.delayed(const Duration(seconds: 3));
          apnsToken = await messaging.getAPNSToken();
          print("after delay apnsToken = $apnsToken");
        } else {
          print("apnsToken = $apnsToken");
        }
      } catch (exception) {
        print("exception = $exception");
      }
    }

    fcmToken = await messaging.getToken();

Xcode 15.0.1 Test device: iPhone 15 Pro iOS 17.0.3 iPhone 14 iOS 16.6.1 iPhone 12 mini iOS 16.7.1

Chia-Wei-Du avatar Nov 15 '23 08:11 Chia-Wei-Du

Hi @Chia-Wei-Du, are you by any chance using flavours for your project? If so, can you make sure that you are loading the correct config?

danagbemava-nc avatar Nov 15 '23 10:11 danagbemava-nc

@danagbemava-nc @darshankawar

Finally, I found the problem. This problem is divided into two cases.

Case1: it should wait the result of requestPermission about NotificationSettings then get the apns token. Before few days ago, I did not use requestPermission for NotificationSettings. In that case, I couldn't even get the apns token with running the release version by Xcode. Until yesterday, after I set requestPermission before apns token, I get the apns token successfully.

Case2: using the Sideloadly to put the ipa to real device should check the identity of Team that same with the Signing Team from Xcode. I keep having this problem because I used the identity of Individual from Sideloadly and used the identity of Team from Xcode. I thought TestFlight would also have this problem because after fix the case 1 about requestPermission, I never try the TestFlight again. I just try the Sideloadly and keep encountering the problem of case2

Sorry to bother you all. But I still hope that my replies are helpful to some people. Thanks.

Chia-Wei-Du avatar Nov 16 '23 06:11 Chia-Wei-Du

Looks like you have resolved your issue.

Closing from here.

Thank you

danagbemava-nc avatar Nov 16 '23 11:11 danagbemava-nc