flutterfire
flutterfire copied to clipboard
[firebase_messaging]: Error while generating FCM Token: AbortError: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker
Is there an existing issue for this?
- [X] I have searched the existing issues.
Which plugins are affected?
Messaging
Which platforms are affected?
Web
Description
FCM Token is not registered on first launch of app, and refreshing the page able to get token.
Need to generated FCM Token on the first launch of the app itself. Its works on android, but error is in web app only
Reproducing the issue
firbase-messaging-sw.js
importScripts("https://www.gstatic.com/firebasejs/10.8.1/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/10.8.1/firebase-messaging-compat.js");
firebase.initializeApp({
apiKey: "AIzaSyCkfdY7*****",
authDomain: "*****firebaseapp.com",
projectId: "*****1e46e",
storageBucket: "*****appspot.com",
messagingSenderId: "*****",
appId: "*****fa3ef478c67a6dfc",
measurementId: "*****BRQ"
});
const messaging = firebase.messaging();
main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform,);
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (kDebugMode) {
print('User granted permission: ${settings.authorizationStatus}');
}
try {
String? fcmToken1 = await messaging.getToken(
vapidKey: kIsWeb ? "BKFKHhWelaWv7**********" : null
);
print(fcmToken1);
} catch (e) {
// Handle any exceptions that may occur during token generation
print("Error while generating FCM Token: $e");
}
}
index.html
<script>
window.addEventListener('load', function(ev) {
// Download main.dart.js
_flutter.loader.loadEntrypoint({
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
firebaseMessagingServiceWorker: 'firebase-messaging-sw.js',
},
onEntrypointLoaded: function(engineInitializer) {
engineInitializer.initializeEngine().then(function(appRunner) {
appRunner.runApp();
});
}
});
});
</script>
Firebase Core version
2.28.0
Flutter Version
3.19.5
Relevant Log Output
Error while generating FCM Token: AbortError: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker
Flutter dependencies
Expand Flutter dependencies snippet
Dart SDK 3.3.3
Flutter SDK 3.19.5
medcouncil 1.0.13+13
dependencies:
- animated_text_kit 4.2.2 [flutter characters]
- carousel_slider 4.2.1 [flutter]
- connectivity_plus 5.0.2 [flutter flutter_web_plugins connectivity_plus_platform_interface js meta nm]
- crypto 3.0.3 [typed_data]
- cupertino_icons 1.0.6
- dropdown_button2 2.3.9 [flutter meta]
- firebase_analytics 10.10.0 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter]
- firebase_core 2.28.0 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_messaging 14.8.0 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- firebase_remote_config 4.4.0 [firebase_core firebase_core_platform_interface firebase_remote_config_platform_interface firebase_remote_config_web flutter]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_linkify 6.0.0 [flutter linkify]
- flutter_local_notifications 17.0.0 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone]
- flutter_native_splash 2.4.0 [args flutter flutter_web_plugins html image meta path universal_io xml yaml ansicolor]
- flutter_pdfview 1.3.2 [flutter]
- fluttericon 2.0.0 [flutter]
- get 4.6.6 [flutter]
- google_fonts 6.2.1 [flutter http path_provider crypto]
- google_nav_bar 5.0.6 [flutter]
- http 1.2.1 [async http_parser meta web]
- in_app_purchase 3.1.13 [flutter in_app_purchase_android in_app_purchase_platform_interface in_app_purchase_storekit]
- internet_connection_checker 1.0.0+1
- multi_select_flutter 4.1.3 [flutter collection]
- package_info_plus 4.2.0 [ffi flutter flutter_web_plugins http meta path package_info_plus_platform_interface win32]
- path 1.9.0
- path_provider 2.1.2 [flutter path_provider_android path_provider_foundation path_provider_linux path_provider_platform_interface path_provider_windows]
- razorpay_web 1.4.2 [flutter flutter_web_plugins eventify package_info_plus universal_platform]
- restart_app 1.2.1 [flutter plugin_platform_interface flutter_web_plugins]
- shared_preferences 2.2.2 [flutter shared_preferences_android shared_preferences_foundation shared_preferences_linux shared_preferences_platform_interface shar
ed_preferences_web shared_preferences_windows]
- sqflite 2.3.3 [flutter sqflite_common path]
- sqflite_common_ffi_web 0.4.3 [path sqflite_common_ffi sqflite_common sqlite3 http web synchronized process_run dev_build pub_semver args meta]
- upgrader 10.0.1 [flutter device_info_plus html http os_detect package_info_plus shared_preferences url_launcher version xml]
- url_launcher 6.2.5 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_l
auncher_windows]
- vimeo_player_flutter 0.1.0 [flutter webview_flutter]
dev dependencies:
- flutter_lints 3.0.1 [lints]
- flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math leak_tracker_flutter_testing async boolean_selector characters coll
ection leak_tracker leak_tracker_testing material_color_utilities meta source_span stream_channel string_scanner term_glyph vm_service]
transitive dependencies:
- _flutterfire_internals 1.3.28 [collection firebase_core firebase_core_platform_interface flutter meta]
- ansicolor 2.0.2
- archive 3.4.10 [crypto path pointycastle]
- 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.18.0
- connectivity_plus_platform_interface 1.2.4 [flutter meta plugin_platform_interface]
- convert 3.1.1 [typed_data]
- csslib 1.0.0 [source_span]
- dbus 0.7.10 [args ffi meta xml]
- dev_build 0.16.4+3 [collection process_run pub_semver yaml path meta pool args]
- device_info_plus 9.1.2 [device_info_plus_platform_interface ffi file flutter flutter_web_plugins meta win32 win32_registry]
- device_info_plus_platform_interface 7.0.0 [flutter meta plugin_platform_interface]
- eventify 1.0.1
- fake_async 1.3.1 [clock collection]
- ffi 2.1.2
- file 7.0.0 [meta path]
- firebase_analytics_platform_interface 3.10.1 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.5.7 [_flutterfire_internals firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins]
- firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.14.0 [firebase_core_platform_interface flutter flutter_web_plugins meta web]
- firebase_messaging_platform_interface 4.5.30 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.8.0 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins meta we
b]
- firebase_remote_config_platform_interface 1.4.28 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_remote_config_web 1.6.0 [firebase_core firebase_core_web firebase_remote_config_platform_interface flutter flutter_web_plugins]
- 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_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math]
- html 0.15.4 [csslib source_span]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- image 4.1.7 [archive meta xml]
- in_app_purchase_android 0.3.2 [collection flutter in_app_purchase_platform_interface json_annotation]
- in_app_purchase_platform_interface 1.3.7 [flutter plugin_platform_interface]
- in_app_purchase_storekit 0.3.13+1 [collection flutter in_app_purchase_platform_interface json_annotation]
- js 0.6.7 [meta]
- json_annotation 4.8.1 [meta]
- leak_tracker 10.0.0 [clock collection meta path vm_service]
- leak_tracker_flutter_testing 2.0.1 [flutter leak_tracker leak_tracker_testing matcher meta]
- leak_tracker_testing 2.0.1 [leak_tracker matcher meta]
- linkify 5.0.0
- lints 3.0.0
- matcher 0.12.16+1 [async meta stack_trace term_glyph test_api]
- material_color_utilities 0.8.0 [collection]
- meta 1.11.0
- nm 0.5.0 [dbus]
- os_detect 2.0.1
- package_info_plus_platform_interface 2.0.1 [flutter meta plugin_platform_interface]
- path_provider_android 2.2.2 [flutter path_provider_platform_interface]
- path_provider_foundation 2.3.2 [flutter path_provider_platform_interface]
- path_provider_linux 2.2.1 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_platform_interface 2.1.2 [flutter platform plugin_platform_interface]
- path_provider_windows 2.2.1 [ffi flutter path path_provider_platform_interface win32]
- petitparser 6.0.2 [meta]
- platform 3.1.4
- plugin_platform_interface 2.1.8 [meta]
- pointycastle 3.7.4 [collection convert js]
- pool 1.5.1 [async stack_trace]
- process_run 0.14.2 [path collection string_scanner yaml meta args pub_semver synchronized]
- pub_semver 2.1.4 [collection meta]
- shared_preferences_android 2.2.1 [flutter shared_preferences_platform_interface]
- shared_preferences_foundation 2.3.5 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.3.2 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.3.2 [flutter plugin_platform_interface]
- shared_preferences_web 2.3.0 [flutter flutter_web_plugins shared_preferences_platform_interface web]
- shared_preferences_windows 2.3.2 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- sky_engine 0.0.99
- source_span 1.10.0 [collection path term_glyph]
- sqflite_common 2.5.4 [synchronized path meta]
- sqflite_common_ffi 2.3.3 [sqlite3 sqflite_common synchronized path meta]
- sqlite3 2.4.0 [collection ffi js meta path]
- stack_trace 1.11.1 [path]
- stream_channel 2.1.2 [async]
- string_scanner 1.2.0 [source_span]
- synchronized 3.1.0+1
- term_glyph 1.2.1
- test_api 0.6.1 [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]
- universal_io 2.2.2 [collection meta typed_data]
- universal_platform 1.0.0+1
- url_launcher_android 6.3.0 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.2.5 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.1.1 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.1.0 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.3.2 [flutter plugin_platform_interface]
- url_launcher_web 2.3.0 [flutter flutter_web_plugins url_launcher_platform_interface web]
- url_launcher_windows 3.1.1 [flutter url_launcher_platform_interface]
- vector_math 2.1.4
- version 3.0.2
- vm_service 13.0.0
- web 0.5.1
- webview_flutter 4.7.0 [flutter webview_flutter_android webview_flutter_platform_interface webview_flutter_wkwebview]
- webview_flutter_android 3.16.0 [flutter webview_flutter_platform_interface]
- webview_flutter_platform_interface 2.10.0 [flutter meta plugin_platform_interface]
- webview_flutter_wkwebview 3.13.0 [flutter path webview_flutter_platform_interface]
- win32 5.3.0 [ffi]
- win32_registry 1.1.2 [ffi win32]
- xdg_directories 1.0.4 [meta path]
- xml 6.5.0 [collection meta petitparser]
- yaml 3.1.2 [collection source_span string_scanner]
Additional context and comments
Seems like its occuring due to race condition. I have browsed the already reported issues, found that
3.5.16 FIX(messaging): fix an issue where Service Worker would not register in time (#11979). (4752efb6)
This issue is been fixed for firebase_messaging_web package, it will be good the same fixed for firebase_messaging too
Thanks for the report, have you tried in release mode?
@Lyokone you mean flutter build web --release or flutter run --release ?
@Lyokone
Executed flutter run --release .Still getting
Error while generating FCM Token: AbortError: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker.
But on inspecting network tab, I could see firebase-messaging-sw.js,firebase-app-compat.js and firebase-messaging-compat.js are loaded status 200.
Update i even updated new packages available on pub.dev
firebase_analytics: ^10.10.1
firebase_core: ^2.29.0
firebase_messaging: ^14.8.1
firebase_remote_config: ^4.4.1
And also, on check on tried to get the token on button click, i could see some 20+more seconds delay then able to getToken printed on console.
Whereas in Android Mobile App, which is already available to users, fectching token within a second or less.
@Lyokone or @TarekkMA Can you update ?
@Lyokone or @TarekkMA Can you update ?
Hi @DDSrivatsan, Still getting the same error, i´m using Flutter 3.19.6 and Dart 3.3.4 with firebase_messaging: 14.9.2 and firebase_core: 2.31.0.
Hi @lautarozanuttini , im waiting for the team to look after this issue. Either @Lyokone or @TarekkMA - should look into this issue.. however you can refresh the page once and try getting the token- it will work
hi, @TarekkMA and @Lyokone can you update ? is this issue fixed ?
@Lyokone or @TarekkMA Can you update ? still the issue is not fixed
Same error, any update @Lyokone
Same mistake. A bit difficult to replicate because it happens sometimes.
I have this button in main..
TextButton(
onPressed: () async {
try {
final response = await FirebaseMessaging.instance.getToken();
print('PushManager $response');
} catch (e) {
print('PushManager error in UI: ${e.toString()}');
}
},
child: const Text('token'),
),
and I see the code that should handle the race condition case... Fixed by https://github.com/firebase/flutterfire/pull/11979
https://github.com/firebase/flutterfire/blob/c6865e477c1b84e8aabef6d5a099eea89c66b427/packages/firebase_messaging/firebase_messaging_web/lib/src/interop/messaging.dart#L59-L66
But, upon seeing that the error message was the same one that needed to be fixed, I took the liberty of manually modifying the package's code to check if that was indeed the message. And yes, as can be seen in the logs, the text of the message is the same, but for some reason, it is still failing.
And these were the logs I obtained.
PushManager error in messaging_web: AbortError: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker firstGetTokenCall: false
PushManager error in messaging_web before rethrow: AbortError: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker firstGetTokenCall: false
PushManager error in UI: AbortError: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker
When the message contains no active service worker, shouldn't it call the method again and retrieve the token?
Suspected Behavior
What I think is happening: The first time, it detects that it is the initial call and enters the conditional for the race condition. It sets firstGetTokenCall to false, calls getToken again, but the service worker is not yet active, causing the error to be thrown. It doesn't enter the condition again because firstGetTokenCall is already false at that point.
cc/ @Lyokone @TarekkMA