Search code examples
angularfirebaseionic-frameworkgoogle-cloud-functionsangularfire

Why does an FCM error appear when calling a firebase function


So I am calling a firebase function through AngularFire like so:

const response = await this.aFunctions.httpsCallable<void, ResponseType>('funcName')().toPromise();

This works when deployed to firebase (hosting), but in the local environment (using ionic serve), it throws this error:

ERROR Error: Uncaught (in promise): FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('http://localhost:8100/firebase-cloud-messaging-push-scope') with script ('http://localhost:8100/firebase-messaging-sw.js'): A bad HTTP response code (404) was received when fetching the script. (messaging/failed-service-worker-registration).
FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('http://localhost:8100/firebase-cloud-messaging-push-scope') with script ('http://localhost:8100/firebase-messaging-sw.js'): A bad HTTP response code (404) was received when fetching the script. (messaging/failed-service-worker-registration).

I am not using FCM in any way in this project. Do you have any clue why this happens?


Solution

  • In my case, I solved it by commenting out the messagingSenderId.

    firebase: {
      apiKey: 'XXXXX',
      authDomain: 'XXXXX',
      databaseURL: 'XXXXX',
      projectId: 'XXXXX',
      storageBucket: 'XXXXX',
      // messagingSenderId: 'XXXXX',   **<== comment out this**
      appId: 'XXXXX'
    }
    

    My case is a bit different and strange. I need to deploy an Angular project to firebase hosting.

    Code:

    const ob = this.functions.httpsCallable('api-name')({});
    ob.subscribe((data)=>{
      //do something here
    });
    

    Error:

    FirebaseError: Messaging: We are unable to register the default service worker.......
    

    Environment:

    • Local: OK
    • firebase hosting default Domain(projectName.web.app): Error
    • firebase hosting default Domain(projectName.firebaseapp.com): OK

    I don't know the reason, but hope it can help you and others who have the similar problem.