Skip to content

Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker #6013

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

Closed
daedwards767 opened this issue Feb 16, 2022 · 4 comments

Comments

@daedwards767
Copy link

[REQUIRED] Describe your environment

  • Operating System version: Windows 10 Home 19043.1526
  • Browser version: 98.0.4758.82 (Official Build) (64-bit)
  • Firebase SDK version: ^9.0.0-beta.1
  • Firebase Product: messaging

[REQUIRED] Describe the problem
Calling messaging getToken sometimes throws the error: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker

This occurs when a user has not yet registered the service worker i.e. it is their first visit to the web app or their first usage of a feature that involves the use of the service worker -> since getToken does not register the SW until that point.

It seems there may be a race condition where the service worker may or may not be ready when
const subscription = await swRegistration.pushManager.getSubscription(); is called here

const subscription = await swRegistration.pushManager.getSubscription();

Other online sources here https://stackoverflow.com/questions/42063006/failed-to-subscribe-the-user-domexception-subscription-failed-no-active-serv and here bradtraversy/node_push_notifications#1 (comment) seem to solve this issue with the line await navigator.serviceWorker.ready; to wait for the service worker to be ready,

This may be connected to issue 5797 here #5797.

Steps to reproduce:

Caveat: in most cases the steps below will not trigger the issue, it will only occur when the service worker is not ready

  1. Clear cache & remove notification permission
  2. Add firebase-messaging-sw.js to root of domain
  3. Call firebase initializeApp
  4. Call firebase getMessaging
  5. Ask and approve browser notification permission
  6. Add firebase onMessage listener
  7. Call firebase await getToken(messaging, { vapidKey: <VAPID_KEY> })

Relevant Code:

const supported = await isSupported()
  if (!supported) {
      throw new Error(NOTIFICATIONS_NOT_SUPPORTED)
  }
  const messaging = getMessaging();
  const messagingToken = await getToken(messaging, {vapidKey})
@jbalidiong
Copy link
Contributor

Hi @daedwards767, thanks for reaching out. We'll try to investigate this and see what we can find.

@jbalidiong
Copy link
Contributor

I tried replicating and I wasn't able to reproduce the same behavior. I noticed that you are using an outdated SDK version, would you mind trying it out with the version 9.6.7 which is the latest for the official release. Would you mind trying to update the SDK version that you're using and see if the issue persists? If I can replicate the issue, I can have a better look into it. Please share a minimal, but complete sample of a project that I can run locally.

@google-oss-bot
Copy link
Contributor

Hey @daedwards767. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 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
Copy link
Contributor

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

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

@firebase firebase locked and limited conversation to collaborators Apr 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants