Skip to content

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

@runtimeZero

Description

@runtimeZero

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions