firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.69k stars 3.97k forks source link

Getting error errors.dart:327 Uncaught (in promise) Error: AbortError: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker on web browser first time. #12415

Closed AbhayJadoun closed 7 months ago

AbhayJadoun commented 8 months ago

I am getting error errors.dart:327 Uncaught (in promise) Error: AbortError: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker

fcmToken = kIsWeb ? await _messaging.getToken( vapidKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') ?? '' : await _messaging.getToken() ?? '';

when kIsWeb is true and code is in main.dart file. Any help would be appreciated.

Lyokone commented 8 months ago

Hello, can you check this issue to see if it helps? Otherwise, I'm going to need a full example app reproducing the issue.

AbhayJadoun commented 8 months ago

@Lyokone I don't have any button on my main.dart and also not required on that page as per my requirement. Then any other workaround for this scenario?? working perfectly on mobile but facing issue only for web browser.

Lyokone commented 8 months ago

As said previously, I'll need a full example app for reproducing the issue to understand what is happening.

google-oss-bot commented 7 months ago

Hey @AbhayJadoun. 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 commented 7 months ago

Since there haven't been any recent updates here, I am going to close this issue.

@AbhayJadoun 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.

DDSrivatsan commented 6 months ago

here is the service worker file - firebase-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: "", authDomain: "", projectId: "", storageBucket: "", messagingSenderId: "", appId: "1", measurementId: "" });

const messaging = firebase.messaging();

below the index.html file - where firebase-messaging-sw.js is registered, and also no firebase sdk/js is included here as they are automatically injected by the flutterfire/cli code

login.dart file

String? fcmToken;

      try {
        fcmToken = await FirebaseMessaging.instance.getToken(
          vapidKey: kIsWeb ? "zzzz" : null
        );

        if (fcmToken != null) {
          print("FCM Token: $fcmToken");
          // You can use the FCM token as needed (e.g., send it to your server, save it locally, etc.)
        } else {
          // Handle the case where the FCM token is null
          print("Error: FCM Token is null");
        }
      } catch (e) {
        // Handle any exceptions that may occur during token generation
        print("Error while generating FCM Token: $e");
      }

here is how i trying to get tokem, works perfetly on android, but in web it takes too long to generated the token, when tried on main.dart file itself it throws the mentioned error by @AbhayJadoun . @Lyokone - look out to this issue