Failed to register Service Worker for Firebase Cloud Messaging

Discussão em 'Angular' iniciado por Mike Twain, Setembro 16, 2024.

  1. Mike Twain

    Mike Twain Guest

    I am having trouble registering my Service Worker for Firebase Cloud Messaging in my Angular App on localhost. My localhost server is running on https connection, i created the key and certificate over openssl for that purpose.

    Getting the following error in my console of Google Chromse Browser (same error in Firefox):

    DOMException: Failed to register a ServiceWorker for scope ('https://localhost:4200/assets/') with script ('https://localhost:4200/assets/firebase-messaging-sw.js'): An unknown error occurred when fetching the script.


    even so in the network tab it looks like he gets something from the request.

    [​IMG]

    But the Response is saying:

    Failed to load response data: Connection is closed, cant dispatch pending call to Network.getResponseBody


    i am trying to register the Service Worker here:

    if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/assets/firebase-messaging-sw.js')
    .then(registration => {
    console.log('Service worker is active:', registration.active);

    if (Notification?.permission === 'granted') {
    console.log('permission granted');
    return this.getFCMToken();
    };
    console.log('permission undefined, default or denied');
    return this.permissionRequestFromUserDevice();
    })
    .catch(error => {
    console.error('Error getting service worker registration:', error);
    });
    }


    my Angular.json assets:

    "assets": [
    "src/favicon.ico",
    "src/assets",
    "src/firebase-messaging-sw.js"
    ],


    firebase-messaging-sw.js is stored under assets and looks like from the documentation with my credentials for my app of course:

    import { initializeApp } from "firebase/app";
    import { getMessaging } from "firebase/messaging/sw";

    const firebaseApp = initializeApp({
    apiKey: 'api-key',
    authDomain: 'project-id.firebaseapp.com',
    databaseURL: 'https://project-id.firebaseio.com',
    projectId: 'project-id',
    storageBucket: 'project-id.appspot.com',
    messagingSenderId: 'sender-id',
    appId: 'app-id',
    measurementId: 'G-measurement-id',
    });


    const messaging = getMessaging(firebaseApp);


    Does anyone have a clue whats wrong with my setup and can help me out?

    Continue reading...

Compartilhe esta Página