Firebase Messaging Error Unable to register the default service worker

I am having the file firebase-messaging-sw.js, which is visible in the networks, logging message in the console too. But sill I am getting :

An error occurred while retrieving token.  FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. (messaging/failed-serviceworker-registration).

I have tried to check the console and also debug, but not sure what is error is.

the firebase-messaging-sw.js file contains:

console.log("in sw page");

the index.html is having:

                apiKey: "XXXXXX",
                authDomain: "",
                databaseURL: "XXXXXXX",
                projectId: "XXXXXX",
                storageBucket: "",
                messagingSenderId: "XXXXXXX",
                appId: "XXXXX"
                //  // Initialize Firebase
                const messaging = firebase.messaging();
            Notification.requestPermission().then((permission) => {
              if (permission === 'granted') {
                console.log('Notification permission granted.');
                    messaging.getToken().then((currentToken) => {
                      if (currentToken) {
                      } else {
                        // Show permission request.
                        console.log('No Instance ID token available. Request permission to generate one.');

                    }).catch((err) => {
                      console.log('An error occurred while retrieving token. ', err);
              } else {
                console.log('Unable to get permission to notify.');

I’m expecting to get a token once the user clicks on allow. What could be the reason? And any resolution for it?


create firebase-messaging-sw.js in your root folder and paste the code in it.

if ('serviceWorker' in navigator) {
  .then(function(registration) {
    console.log('Registration successful, scope is:', registration.scope);
  }).catch(function(err) {
    console.log('Service worker registration failed, error:', err);

Leave a Reply