-
Notifications
You must be signed in to change notification settings - Fork 892
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
getToken throw error : DOMException: Failed to execute 'subscribe' on 'PushManager': Subscription failed - no active Service Worker #7693
Comments
having the same issue. it seems the service worker file |
I have similar issue as well. |
So I took this temporary approach to the problem.
I look forward to letting me know when this issue is completely fixed |
Facing same issue with latest firebase 10.5.0. Please help with the fix. |
@dpeese this solution worked for me
|
Running into the same issue. So is the recommended solution to just retry if it fails? |
register Firebase service worker before getToken: |
Hi bro. I am also facing a similar issue as you. And I fix it as follows.
|
https://github.com/benixal/FCM-Web-Notify-Example/blob/main/index.html#L35 |
I resolve it by waiting for activated state of ServiceWorker. const getFBToken = (
m: Messaging,
serviceWorkerRegistration: ServiceWorkerRegistration
): Promise<string> | undefined => {
let sw: ServiceWorker | undefined;
if (serviceWorkerRegistration.installing) {
sw = serviceWorkerRegistration.installing;
} else if (serviceWorkerRegistration.waiting) {
sw = serviceWorkerRegistration.waiting;
} else if (serviceWorkerRegistration.active) {
sw = serviceWorkerRegistration.active;
}
if (sw) {
if (sw.state === 'activated') {
return getToken(m, {
vapidKey: import.meta.env.VITE_PUSH_PUBLIC_KEY,
serviceWorkerRegistration
});
}
const deferred = new Deferred<string>();
sw.addEventListener('statechange', async (e) => {
if ((e.target as ServiceWorker)?.state === 'activated') {
const token = await getToken(m, {
vapidKey: import.meta.env.VITE_PUSH_PUBLIC_KEY,
serviceWorkerRegistration
});
deferred.resolve(token);
}
});
return deferred.promise;
}
return undefined;
}; |
I resolved it like this export async function generateToken({ subscribeTopic }: { subscribeTopic: (token: string) => void }) { |
I tried registration, marking as ready, doing 3 ways, but nothing helped. Just try to get FCM Token multiple times and everything would be fine 💯
|
This worked for me: const registration = await navigator.serviceWorker.register("sw.js")
// Wait for serviceWorker.ready
await navigator.serviceWorker.ready
const currentToken = await getToken(messaging, {
serviceWorkerRegistration: registration,
vapidKey: '...'
}) |
This line is problematic if you have another service worker installed (as it should be the case for PWA apps), because it will not wait for the service you just registered, it will wait for the 1st one serving that page. To fix you're going to have to wait on that SPECIFIC service worker. I know that there was a proposal to add that method to the spec, but I couldn't find it. w3c/ServiceWorker#1278 (comment) function registerReady(swScript, options) {
return navigator.serviceWorker.register(swScript, options).then(reg => {
// If there is an active worker and nothing incoming, we are done.
var incomingSw = reg.installing || reg.waiting;
if (reg.active && !incomingSw) {
return Promise.resolve();
}
// If not, wait for the newest service worker to become activated.
return new Promise(fulfill => {
incomingSw.onstatechange = evt => {
if (evt.target.state === 'activated') {
incomingSw.onstatechange = null;
return fulfill();
}
};
});
})
} And then use it: registerReady("/firebase-messaging-sw.js", { scope: "/firebase-cloud-messaging-push-scope" }).then(function (registration) {
//get token
messaging.getToken({ vapidKey: '@Constants.FirebaseVapId', serviceWorkerRegistration: registration }).then(function (currentToken) {
if (currentToken) {
console.log('Generated new Firebase token: ' + currentToken);
}
})
.catch(function (err) {
console.log('Failed subscribing push notifications.', err);
});
}); |
its work for me like this i add service worker registration as params to getToken function |
@Poucaslontras as is see it your solution in #7693 (comment) does not work as-is, because the Promise returned by |
Not sure what do you mean, as this is solution works for us as is, and is currently in PROD. |
@Poucaslontras the so when using the function like this (as in your example): to make the returned Promise always resolve with a ServiceWorkerRegistration object, it should look something like this:
|
I agree with @schellmax - the promise should return the registration - otherwise the logic is correct and fixes the issue so thank you @Poucaslontras! Typescript example: async function registerReady(scriptURL: string, options?: RegistrationOptions) {
return navigator.serviceWorker
.register(scriptURL, options)
.then((registration) => {
// If there is an active worker and nothing incoming, we are done.
const incomingSw = registration.installing || registration.waiting;
if (registration.active && !incomingSw) return Promise.resolve(registration);
// If not, wait for the newest service worker to become activated.
return new Promise<ServiceWorkerRegistration>((fulfill, reject) => {
if (incomingSw) {
incomingSw.onstatechange = (evt) => {
if ((evt.target as ServiceWorker)?.state === 'activated') {
incomingSw.onstatechange = null;
return fulfill(registration);
}
};
} else {
reject(new Error('No incoming service worker found.'));
}
});
})
.catch((err) => {
console.error('Error registering service worker:', err);
return Promise.reject(err);
});
} And it can be called with async/await if you wish: const registration = await registerReady('url', {}); |
Operating System
win 10
Browser Version
chrome 118.0.5993.71 (Official Build) (64-bit)
Firebase SDK Version
10.4.0
Firebase SDK Product:
Messaging
Describe your project's tooling
https://www.gstatic.com/firebasejs/10.4.0/firebase-app-compat.js
https://www.gstatic.com/firebasejs/10.4.0/firebase-messaging-compat.js
Describe the problem
clear cookie
when open my web site for the first time, getToken throw the following error【NG】
when open my web site for the second time or refresh the tab, getToken works fine.
Steps and code to reproduce issue
The text was updated successfully, but these errors were encountered: