quickstart-js icon indicating copy to clipboard operation
quickstart-js copied to clipboard

Including service worker file "firebase-messaging-sw.js"

Open runtimeZero opened this issue 9 years ago • 10 comments

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

  1. Include the service worker file under scripts in angular-cli.json ... does NOT work See Screenshot

  2. Generate a distribution of the app and then MANUALLY place the service worker file in the dist folder

See Screenshot

THIS WORKS! BUT is not a feasible for two reasons

  1. during development as I cannot use the files in dist/ folder
  2. 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 avatar Jul 16 '17 11:07 runtimeZero

@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:

  1. Figure out how to get the file serving from the root of your origin
  2. 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

gauntface avatar Jul 17 '17 15:07 gauntface

issue resolved.. kindly close this ticket

shreya-work avatar Jul 21 '17 14:07 shreya-work

@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: image

reyou avatar Sep 21 '17 03:09 reyou

@reyou I raised an issue against the actual codelab here: https://github.com/firebase/friendlychat-web/issues/245

gauntface avatar Sep 26 '17 21:09 gauntface

Thank you @gauntface!

reyou avatar Sep 26 '17 21:09 reyou

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();

badpaybad avatar Jul 11 '18 23:07 badpaybad

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.

zihad15 avatar Dec 26 '18 15:12 zihad15

@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 ??

medletayef avatar Mar 31 '21 10:03 medletayef

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(().....

Arkovski avatar Jun 11 '24 09:06 Arkovski

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.

am1goo avatar Feb 21 '26 08:02 am1goo