Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [firebase_messaging] .onTokenRefresh not called even when it actually got refreshed #10209

Closed
Shawn-sudo opened this issue Jan 3, 2023 · 6 comments
Labels
resolution: user This was a user issue, e.g. invalid configuration or code. type: bug Something isn't working

Comments

@Shawn-sudo
Copy link

Bug report

FirebaseMessaging.instance.onTokenRefresh.listen((newToken) {...}) is not called even in the expected situation (app running on the foreground, internet connected, foreground notification implemented, Firebase Messaging backend starting to throw "Requested entity was not found" error)

As a result, users (me) suddenly became unavailable to receive notifications while using the app.
Once I quit the app and relaunch it, the regenerated token is well updated by calling FirebaseMessaging.instance.getToken()
Until I relaunch the app, the backend throws "Requested entity was not found" error and I become unavailable to receive notification

Check this video for more info: https://vimeo.com/785835642

Steps to reproduce

(It's gonna be a bit tricky, cause the app should be opened when the token is regenerated)
(I found this bug on iOS. I'm not sure if this also happens on other platforms)

  1. Launch the app
  2. The app should have some codes to .getToken() and update it
  3. The app should also initialize FirebaseMessaging.instance.onTokenRefresh properly by .listen()ing to it
  4. Wait
  5. Wait
  6. Wait
  7. Wait
  8. One day, it suddenly becomes unavailable to receive notifications. When I check the logs, .onTokenRefresh hasn't been called. When I relaunch the app, .getToken() gives me a different token from the one that was previously registered.

Expected behavior

.onTokenRefresh should be called when the token is regenerated


Additional context

Nope


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.10, on macOS 13.1 22C65 darwin-arm, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] VS Code (version 1.74.2)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

• No issues found!

Flutter dependencies

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

Click To Expand

Dart SDK 2.18.6
Flutter SDK 3.3.10
atrable 1.3.0

dependencies:
- audio_session 0.1.13 [flutter flutter_web_plugins rxdart meta]
- build_runner 2.3.2 [args async analyzer build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style frontend_server_client glob graphs http_multi_server io js logging meta mime package_config path pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml]
- camera 0.10.1 [camera_android camera_avfoundation camera_platform_interface camera_web flutter flutter_plugin_android_lifecycle quiver]
- cloud_firestore 4.3.0 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- cupertino_icons 1.0.5
- emoji_picker_flutter 1.5.1 [flutter flutter_web_plugins plugin_platform_interface shared_preferences]
- eraser 2.0.1 [flutter]
- ffmpeg_kit_flutter 4.5.1 [ffmpeg_kit_flutter_platform_interface flutter]
- firebase_auth 4.2.3 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 2.4.0 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_database 10.0.8 [firebase_core firebase_core_platform_interface firebase_database_platform_interface firebase_database_web flutter]
- firebase_messaging 14.2.0 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- firebase_storage 11.0.9 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_emoji 2.4.0 [characters http]
- flutter_svg 1.1.6 [flutter meta path_drawing vector_math xml]
- flutter_video_info 1.3.1 [flutter path]
- image_cropper 3.0.1 [flutter image_cropper_platform_interface image_cropper_for_web]
- image_picker 0.8.6 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_platform_interface]
- infinite_scroll_pagination 3.2.0 [flutter sliver_tools]
- overlay_support 2.0.1 [flutter async]
- path_provider 2.0.11 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows]
- permission_handler 10.2.0 [flutter meta permission_handler_android permission_handler_apple permission_handler_windows permission_handler_platform_interface]
- pinch_zoom_image_last 0.4.1 [flutter]
- provider 6.0.3 [collection flutter nested]
- qr_flutter 4.0.0 [flutter qr]
- realm 0.8.0+rc [flutter args build_cli_annotations collection crypto ffi json_annotation logging meta package_config path pubspec_parse pub_semver realm_common realm_generator tar build_runner http cancellation_token]
- record 4.4.3 [flutter record_platform_interface record_web record_windows record_macos record_linux]
- scrolls_to_top 2.0.0 [flutter]
- share_plus 6.3.0 [cross_file meta mime flutter flutter_web_plugins share_plus_platform_interface file url_launcher_web url_launcher_windows url_launcher_linux url_launcher_platform_interface ffi win32]
- shared_preferences 2.0.15 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows]
- shimmer 2.0.0 [flutter]
- sign_in_with_apple 4.3.0 [flutter meta sign_in_with_apple_platform_interface sign_in_with_apple_web]
- transparent_image 2.0.0
- uni_links 0.5.1 [flutter uni_links_platform_interface uni_links_web]
- url_launcher 6.1.7 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows]
- video_player 2.4.10 [flutter html video_player_android video_player_avfoundation video_player_platform_interface video_player_web]
- video_thumbnail 0.5.3 [flutter]
- wave 0.2.2 [flutter]

dev dependencies:
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph]

transitive dependencies:
- _fe_analyzer_shared 47.0.0 [meta]
- _flutterfire_internals 1.0.11 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- analyzer 4.7.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml]
- args 2.3.1
- async 2.9.0 [collection meta]
- boolean_selector 2.1.0 [source_span string_scanner]
- build 2.3.1 [analyzer async convert crypto glob logging meta path]
- build_cli_annotations 2.1.0 [args meta]
- build_config 1.1.1 [checked_yaml json_annotation path pubspec_parse yaml]
- build_daemon 3.1.0 [built_collection built_value http_multi_server logging path pool shelf shelf_web_socket stream_transform watcher web_socket_channel]
- build_resolvers 2.0.10 [analyzer async build crypto graphs logging path package_config pool pub_semver stream_transform yaml]
- build_runner_core 7.2.7 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta path package_config pool timing watcher yaml]
- built_collection 5.1.1
- built_value 8.4.2 [built_collection collection fixnum meta]
- camera_android 0.10.0+3 [camera_platform_interface flutter flutter_plugin_android_lifecycle stream_transform]
- camera_avfoundation 0.9.8+6 [camera_platform_interface flutter stream_transform]
- camera_platform_interface 2.2.2 [cross_file flutter plugin_platform_interface stream_transform]
- camera_web 0.3.0+1 [camera_platform_interface flutter flutter_web_plugins stream_transform]
- cancellation_token 1.5.0 [meta collection]
- characters 1.2.1
- checked_yaml 2.0.1 [json_annotation source_span yaml]
- clock 1.1.1
- cloud_firestore_platform_interface 5.10.0 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 3.2.0 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]
- code_builder 4.3.0 [built_collection built_value collection matcher meta]
- collection 1.16.0
- convert 3.1.1 [typed_data]
- cross_file 0.3.3+2 [js meta]
- crypto 3.0.2 [typed_data]
- csslib 0.17.2 [source_span]
- dart_style 2.2.4 [analyzer args path pub_semver source_span]
- fake_async 1.3.1 [clock collection]
- ffi 2.0.1
- ffmpeg_kit_flutter_platform_interface 0.2.1 [flutter plugin_platform_interface]
- file 6.1.4 [meta path]
- firebase_auth_platform_interface 6.11.6 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 5.2.3 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta]
- firebase_core_platform_interface 4.5.2 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.0.2 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_database_platform_interface 0.2.2+16 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_database_web 0.2.1+18 [firebase_core firebase_core_web firebase_database_platform_interface flutter flutter_web_plugins js]
- firebase_messaging_platform_interface 4.2.9 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.2.10 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- firebase_storage_platform_interface 4.1.25 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_storage_web 3.3.18 [_flutterfire_internals async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta]
- fixnum 1.0.1
- flutter_plugin_android_lifecycle 2.0.7 [flutter]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- frontend_server_client 3.2.0 [async path]
- glob 2.1.1 [async collection file path string_scanner]
- graphs 2.2.0 [collection]
- html 0.15.0 [csslib source_span]
- http 0.13.5 [async http_parser meta path]
- http_multi_server 3.2.1 [async]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- image_cropper_for_web 1.0.3 [flutter flutter_web_plugins image_cropper_platform_interface js]
- image_cropper_platform_interface 3.0.3 [flutter plugin_platform_interface http]
- image_picker_android 0.8.5+3 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface]
- image_picker_for_web 2.1.10 [flutter flutter_web_plugins image_picker_platform_interface]
- image_picker_ios 0.8.6+1 [flutter image_picker_platform_interface]
- image_picker_platform_interface 2.6.2 [cross_file flutter http plugin_platform_interface]
- intl 0.17.0 [clock path]
- io 1.0.3 [meta path string_scanner]
- js 0.6.4
- json_annotation 4.7.0 [meta]
- logging 1.1.0
- matcher 0.12.12 [stack_trace]
- material_color_utilities 0.1.5
- meta 1.8.0
- mime 1.0.2
- nested 1.0.0 [flutter]
- objectid 2.1.0
- package_config 2.1.0 [path]
- path 1.8.2
- path_drawing 1.0.1 [vector_math meta path_parsing flutter]
- path_parsing 1.0.1 [vector_math meta]
- path_provider_android 2.0.20 [flutter path_provider_platform_interface]
- path_provider_ios 2.0.11 [flutter path_provider_platform_interface]
- path_provider_linux 2.1.7 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_macos 2.0.6 [flutter path_provider_platform_interface]
- path_provider_platform_interface 2.0.5 [flutter platform plugin_platform_interface]
- path_provider_windows 2.1.3 [ffi flutter path path_provider_platform_interface win32]
- permission_handler_android 10.2.0 [flutter permission_handler_platform_interface]
- permission_handler_apple 9.0.7 [flutter permission_handler_platform_interface]
- permission_handler_platform_interface 3.9.0 [flutter meta plugin_platform_interface]
- permission_handler_windows 0.1.2 [flutter permission_handler_platform_interface]
- petitparser 5.0.0 [meta]
- platform 3.1.0
- plugin_platform_interface 2.1.3 [meta]
- pool 1.5.1 [async stack_trace]
- process 4.2.4 [file path platform]
- pub_semver 2.1.3 [collection meta]
- pubspec_parse 1.2.1 [checked_yaml collection json_annotation pub_semver yaml]
- qr 2.1.0 [meta]
- quiver 3.1.0 [matcher]
- realm_common 0.8.0+rc [objectid sane_uuid]
- realm_generator 0.8.0+rc [analyzer build_resolvers build dart_style realm_common source_gen source_span]
- record_linux 0.3.4 [flutter record_platform_interface path]
- record_macos 0.2.2 [flutter record_platform_interface]
- record_platform_interface 0.5.0 [flutter plugin_platform_interface]
- record_web 0.5.0 [flutter flutter_web_plugins record_platform_interface]
- record_windows 0.6.2 [flutter record_platform_interface path]
- rxdart 0.27.5
- sane_uuid 1.0.0-alpha.4 [meta crypto]
- share_plus_platform_interface 3.2.0 [cross_file flutter meta mime plugin_platform_interface path_provider uuid]
- shared_preferences_android 2.0.13 [flutter shared_preferences_platform_interface]
- shared_preferences_ios 2.1.1 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.1.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_macos 2.0.4 [flutter shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.1.0 [flutter plugin_platform_interface]
- shared_preferences_web 2.0.4 [flutter flutter_web_plugins shared_preferences_platform_interface]
- shared_preferences_windows 2.1.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- shelf 1.4.0 [async collection http_parser path stack_trace stream_channel]
- shelf_web_socket 1.0.3 [shelf stream_channel web_socket_channel]
- sign_in_with_apple_platform_interface 1.0.0 [flutter plugin_platform_interface meta]
- sign_in_with_apple_web 1.0.1 [flutter flutter_web_plugins sign_in_with_apple_platform_interface js]
- sky_engine 0.0.99
- sliver_tools 0.2.8 [flutter]
- source_gen 1.2.6 [analyzer async build dart_style glob meta path source_span yaml]
- source_span 1.9.0 [collection path term_glyph]
- stack_trace 1.10.0 [path]
- stream_channel 2.1.0 [async]
- stream_transform 2.0.1
- string_scanner 1.1.1 [source_span]
- tar 0.5.6 [async meta typed_data]
- term_glyph 1.2.1
- test_api 0.4.12 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- timing 1.0.0 [json_annotation]
- typed_data 1.3.1 [collection]
- uni_links_platform_interface 1.0.0 [flutter plugin_platform_interface]
- uni_links_web 0.1.0 [flutter flutter_web_plugins uni_links_platform_interface]
- url_launcher_android 6.0.19 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.1.1 [flutter plugin_platform_interface]
- url_launcher_web 2.0.13 [flutter flutter_web_plugins url_launcher_platform_interface]
- url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface]
- uuid 3.0.6 [crypto]
- vector_math 2.1.2
- video_player_android 2.3.9 [flutter video_player_platform_interface]
- video_player_avfoundation 2.3.7 [flutter video_player_platform_interface]
- video_player_platform_interface 5.1.4 [flutter plugin_platform_interface]
- video_player_web 2.0.12 [flutter flutter_web_plugins video_player_platform_interface]
- watcher 1.0.2 [async path]
- web_socket_channel 2.2.0 [async crypto stream_channel]
- win32 3.0.0 [ffi]
- xdg_directories 0.2.0+2 [meta path process]
- xml 6.1.0 [collection meta petitparser]
- yaml 3.1.1 [collection source_span string_scanner]

@Shawn-sudo Shawn-sudo added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Jan 3, 2023
@darshankawar darshankawar added the triage Issue is currently being triaged. label Jan 3, 2023
@darshankawar
Copy link

Thanks for the report @Shawn-sudo
Does it only happen when you are installing the app for the first time ? or every time when you run the app ?
Can you try the plugin example and see if using it, you get same behavior ?

8. One day, it suddenly becomes unavailable to receive notifications.

There's no code changes or updates related to token recently, so if you are seeing this behavior intermittently, then it could be a server or native side issue.

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Jan 3, 2023
@Shawn-sudo
Copy link
Author

It happens occasionally: not necessarily after the first install.
Once the token gets refreshed while the app is on foreground without .onTokenRefresh.listen() being called, I don't receive any further notifications until I reopen the app and get the refreshed token.

I have no clue how long it would take to reproduce this issue. It sometimes take a week, sometime take an hour.
Do you know if there's a way to force refreshing the FCM token from the backend so that we can test if .onTokenRefresh.listen() and my code inside it is working well?

I'm quite sure that mine should be working well, but just in case.


Seeing this source code here, I'm guessing that .onTokenRefresh is called every time when .didReceiveRegistrationToken is called. Then I saw this comment which says // Don't crash if the token is reset. I think this is somehow related to the issue that I'm facing, but not really sure.


By the way, one thing to mention, the fcm token for my phone changes at least once in a day. This StackOverflow Answer says that key rotation is such a rare event, so I think something else is going wrong on my app, too.


I'm trying the plugin example rn. I'll come back when I find the issue

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Jan 5, 2023
@darshankawar
Copy link

Thanks for the update.

Do you know if there's a way to force refreshing the FCM token from the backend so that we can test if .onTokenRefresh.listen() and my code inside it is working well?

Maybe not from the backend, but one of the ways is to delete the fcm token and then request for it again which will provide a new token.

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Jan 5, 2023
@Shawn-sudo
Copy link
Author

@darshankawar Oh thanks
Then should I expect .onTokenRefresh.listen() to be called once I delete the fcm token and call .getToken()?

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Jan 5, 2023
@darshankawar
Copy link

Then should I expect .onTokenRefresh.listen() to be called once I delete the fcm token and call .getToken()?

Ideally it should be called. So, if you are deleting the token like below and then call getToken():

Future<void> refreshToken() async {
    await FirebaseMessaging.instance.deleteToken();
    await FirebaseMessaging.instance.getToken();
  }

And then:

tokenSubscriptionStream = FirebaseMessaging.instance.onTokenRefresh.listen(
          (String _token) {
        print("NEW TOKEN: $_token");
        setState(() => token = _token);
      },
    );

where tokenSubscriptionStream is late StreamSubscription<String>

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Jan 6, 2023
@Shawn-sudo
Copy link
Author

@darshankawar
Hi
.onTokenRefresh was called well as expected once I delete the token and .getToken() again.

One thing I noticed, when I run the app on my phone using Flutter release mode through Xcode, it seems to disable receiving the notifications. Probably I was confused with that.

Thanks!

@darshankawar darshankawar added resolution: user This was a user issue, e.g. invalid configuration or code. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. triage Issue is currently being triaged. labels Jan 9, 2023
@firebase firebase locked and limited conversation to collaborators Feb 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
resolution: user This was a user issue, e.g. invalid configuration or code. type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants