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

FCM "Messaging: We are unable to register the default service worker. The operation is insecure. (messaging/failed-serviceworker-registration)."

Open bhritch opened this issue 8 years ago • 38 comments

Help,

would like to ask, why i am getting this error, but in my local it's working fine. I check my firebase-messaging-sw.js and it's there. what would be the cause?

error message: code: "messaging/failed-serviceworker-registration" Messaging: We are unable to register the default service worker. The operation is insecure. (messaging/failed-serviceworker-registration).

thanks

bhritch avatar Jun 07 '18 10:06 bhritch

this file is missing firebase-messaging-sw.js. please add firebase-messaging-sw.js file in root folder.

i think in server /var/www/... something it worked for me

vshreyas-tricog avatar Jun 07 '18 11:06 vshreyas-tricog

it's in my root, when i type http://.com/firebase-messaging-sw.js it shows the file content. but it didn't work for me

bhritch avatar Jun 07 '18 11:06 bhritch

image

can you check in dev-tools--->application-->service-workers is any worker is active.

like above image

vshreyas-tricog avatar Jun 07 '18 11:06 vshreyas-tricog

// Give the service worker access to Firebase Messaging. // Note that you can only use Firebase Messaging here, other Firebase libraries // are not available in the service worker. importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js'); // importScripts('/__/firebase/init.js');

// Initialize the Firebase app in the service worker by passing in the // messagingSenderId. firebase.initializeApp({ 'messagingSenderId': 'SENDER_ID' });

// Retrieve an instance of Firebase Messaging so that it can handle background // messages. firebase.messaging();

firebae-messing-sw.js file

vshreyas-tricog avatar Jun 07 '18 11:06 vshreyas-tricog

You can only use service workers if you're serving your code via HTTPS, except when you're serving from localhost. See here for more details.

"You’ll also need to serve your code via HTTPS — Service workers are restricted to running across HTTPS for security reasons." and "In order to facilitate local development, localhost is considered a secure origin by browsers as well."

mmermerkaya avatar Jun 14 '18 17:06 mmermerkaya

It does not work with Firefox or Safari, just Chrome supports localhost:"ANYPORT". If you try to substitute with ngrok it does not work either, and you get the error stated in the issue.

dkdndes avatar Jun 08 '19 15:06 dkdndes

Please check:

  • https://bugzilla.mozilla.org/show_bug.cgi?id=1429714

If cookies are "Kept until I close Firefox," it will not work. You need to check if the option is switched on.

What I don't understand is why do I need this option (and save the cookie); if I get a new token whenever I start an FCM session?

dkdndes avatar Jun 13 '19 12:06 dkdndes

@dkdndes please open a new issue if you're having this problem!

samtstern avatar Jun 13 '19 16:06 samtstern

Unable to get permission to notify. 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).

Any one got solution for this ???

pullatjunaid avatar Oct 09 '19 09:10 pullatjunaid

Where did you put your firebase-messaging-sw.js?

anubhavkamath avatar Oct 13 '19 21:10 anubhavkamath

Unable to get permission to notify. 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).

Any one got solution for this ???

You need to make sure the firebase-messaging-sw.js has existed in your public folder

tridungle avatar Oct 21 '19 08:10 tridungle

Unable to get permission to notify. 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).

Any one got solution for this ???

yea have you found the solution??

bhoopk avatar Dec 18 '19 21:12 bhoopk

you need to pass whole project config when doing

firebase.initializeApp({
  apiKey: 'xxxx',
  authDomain: 'xxxx.firebaseapp.com',
  databaseURL: 'https://xxxx.firebaseio.com',
  projectId: 'xxxx',
  storageBucket: 'xxxx.appspot.com',
  messagingSenderId: 'xxxx',
  appId: 'xxxx'
});

parth181195 avatar Jan 17 '20 08:01 parth181195

hi in localhost i got both foreground and background notification but in production (https) i only got the background notification using FIREBASE 7.6.0 service worker also same

karthik11788 avatar May 28 '20 11:05 karthik11788

Works like a charm. Thanks @parth181195. 👍

Sakthivelgovindan avatar Mar 19 '21 10:03 Sakthivelgovindan

For any one else stuck on this, my error was similar to this but slightly different. It stemmed from a problem with the firebase-messaging-sw.js file not being publicly accessible. This comment explains it clearly.

I fixed mine by moving the firebase-messaging-sw.js file up one folder so it was accessible via http://localhost:<PORT NUMBER>/firebase-messaging-sw.js instead of http://localhost:<PORT NUMBER>/messaging/firebase-messaging-sw.js

RinwaOwuogba avatar Mar 30 '21 22:03 RinwaOwuogba

@parth181195 @mmermerkaya 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 project config . Unlike Chrome and Edge , it worked 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

If you're working with React and you've put your fireabse-messaging-sw.js in the public folder you cannot use proces.env to fetch the values for firebaseConfig (apiKey, authDomain, etc.), provided that you have stored them as an environment variable.

urosran avatar May 10 '21 03:05 urosran

If you are using typescript with React, just put your firebase-messaging-sw.js in your public folder. And remember to turn "allowJs": true, in your tsconfig.json under compilerOptions.

Your firebase-messaging-sw.js should look like this:

`// Scripts for firebase and firebase messaging importScripts('https://www.gstatic.com/firebasejs/8.2.0/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/8.2.0/firebase-messaging.js');

// Initialize the Firebase app in the service worker by passing the generated config var firebaseConfig = { apiKey: "YOUR_API_KEY", authDomain: "YOUR_AUTH_DOMAIN", projectId: "YOUR_PROJECT_ID", storageBucket: "YOUR_STORAGE_BUCKET", messagingSenderId: "YOUR_MESSAGING_SENDER_ID", appId: "YOUR_APP_ID" };

firebase.initializeApp(firebaseConfig);

// Retrieve firebase messaging const messaging = firebase.messaging();

messaging.onBackgroundMessage(function(payload) { console.log('Received background message ', payload);

const notificationTitle = payload.notification.title; const notificationOptions = { body: payload.notification.body, };

self.registration.showNotification(notificationTitle, notificationOptions); });`

bruninhoms avatar May 17 '21 14:05 bruninhoms

The script has an unsupported MIME type ('text/html'). ==> Service worker registration failed, error: DOMException: Failed to register a ServiceWorker for scope ('http://localhost:3000/') with script ('http://localhost:3000/firebase-messaging-sw.js'): The script has an unsupported MIME type ('text/html').

Getting this error

pradipvadher avatar Sep 06 '21 11:09 pradipvadher