Including service worker file "firebase-messaging-sw.js"
As per the guidelines, in order for firebase messaging to function, the file file "firebase-messaging-sw.js" should be included at root level.
As you can see from this screenshot, I have included the same
The firebase app get's initialized in my app.component.ts and all of it's services work fine. However, the minute I try to fetch a messaging token .. i get the following error:
user.service.ts:26 ERROR requesting token FirebaseError {code: "messaging/failed-serviceworker-registration", message: "Messaging: We are unable to register the default s…t. (messaging/failed-serviceworker-registration).", browserErrorMessage: "Failed to register a ServiceWorker: A bad HTTP res…code (404) was received when fetching the script.", __zone_symbol__currentTask: ZoneTask, stack: "FirebaseError: Messaging: We are unable to registe…ttp://localhost:4200/polyfills.bundle.js:2776:47)"}browserErrorMessage: "Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script."code: "messaging/failed-serviceworker-registration"message: "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)."__zone_symbol__currentTask: ZoneTaskstack: "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).↵ at http://localhost:4200/vendor.bundle.js:96127:44↵
The problem as I understand is that the firbase messaging system is unable to locate the file, firebase-messaging-sw.js
SOLUTIONS TRIED
-
Include the service worker file under scripts in angular-cli.json ... does NOT work See Screenshot
-
Generate a distribution of the app and then MANUALLY place the service worker file in the dist folder
THIS WORKS! BUT is not a feasible for two reasons
- during development as I cannot use the files in dist/ folder
- Very risky in production as all file in dist folder are deleted and recreated by angular-cli and if one forgets to MANUALLY add the service worker file, then the entire messaging system will fail to work in production
Does anyone know of a solid solution for this issue ?
@runtimeZero when you run your server, try loading up http://localhost:<PORT NUMBER>/firebase-messaging-sw.js, if that URL cannot be loaded, then you'll get that error. I'd recommend one of the following things to fix it:
- Figure out how to get the file serving from the root of your origin
- Make use of
useServiceWorker()to register a service worker in some other location that will work and then get the messaging SDK to use that.: https://firebase.google.com/docs/reference/js/firebase.messaging.Messaging#useServiceWorker
issue resolved.. kindly close this ticket
@gauntface I am having same exact problem at my local machine. I am following same steps at https://codelabs.developers.google.com/codelabs/firebase-web/#9 I was able to show notification permission at browser and I allowed it, but I am getting same error of @shreya-work
http://localhost:5000/ works but http://localhost:5000/firebase-messaging-sw.js returns
Cannot GET /firebase-messaging-sw.js
error.
Is there any suggestion to have this file enabled? I cannot seem to enable it.
Here is my firebase.json file;
{
"hosting": {
"public": "./",
"ignore": [
"firebase.json",
"database-rules.json",
"storage.rules",
"functions"
],
"headers": [{
"source" : "**/*.@(js|html)",
"headers" : [ {
"key" : "Cache-Control",
"value" : "max-age=0"
} ]
}]
}
}
Error ScreenShot:

@reyou I raised an issue against the actual codelab here: https://github.com/firebase/friendlychat-web/issues/245
Thank you @gauntface!
I do sample here https://github.com/badpaybad/firebase-web-notification . Must need mainfest.json and firebase-messaging-sw.js just copy it and should not modify. And they should be place in root web.
Content inside file firebase-messaging-sw.js importScripts('https://www.gstatic.com/firebasejs/5.2.0/firebase.js'); importScripts('https://www.gstatic.com/firebasejs/5.2.0/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/5.2.0/firebase-messaging.js');
// Initialize Firebase var config = { apiKey: "xxx Your api key xxx", authDomain: "juljulsausau.firebaseapp.com", databaseURL: "https://juljulsausau.firebaseio.com", projectId: "juljulsausau", storageBucket: "juljulsausau.appspot.com", messagingSenderId: "102211866511" };
firebase.initializeApp(config);
var messaging = firebase.messaging();
how if the issues in laravel? I got same issues but in different way, I use laravel and got the issues like this, can you help me to solve? thanks before.
@gauntface
I ve placed my service worker file in root folder and it s accessible in http://localhost:<PORT NUMBER>/firebase-messaging-sw.js also I ve passed the whole firebase project config .
Unlike Chrome and Edge , it worked for me only on Firefox browser
I ve tried this options without a result :
- Update Google Chrome to latest
- Clear ssl cache
- Clear all browser cache and history
- Disabling QUIC Protocol wich provides TLS/SSL equivalent connection
- Reinstall Google Chrome Any Solution ??
For me worked creation a new file and naming it firebase-messaging-sw.js in your wwwroot (or where index.ts is).
importScripts('https://www.gstatic.com/firebasejs/10.12.2/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/10.12.2/firebase-messaging-compat.js');
const app = firebase.initializeApp({
apiKey: "XXXX-XXXX",
authDomain: "XXXX.firebaseapp.com",
projectId: "XXXX-XXXX",
storageBucket: "XXXX.appspot.com",
messagingSenderId: "XXXX",
appId: "1:XXXX"
});
const messaging = firebase.messaging(app);
messaging.onBackgroundMessage(function(payload) {
const notificationOptions = {
title: "",
body: payload.notification.body,
icon: './myIcon.png',
};
self.registration.showNotification(payload.notification.title, notificationOptions);
});
And doing in my-app-configuration.ts:
navigator.serviceWorker.register('/firebase-messaging-sw.js').then(().....
Oh my gosh...
I've experienced the same issue, and @Arkovski peace of advice is works for me.
Empty (no text inside) firebase-messaging-sw.js fixed everything for start.
But... As I can figure out how it works, it completely disables mechanism for receiving push messages.