Use Firebase Messaging (FCM) for React Native Web

Viewed 235

I want to use Firebase Messaging FCM for push notifications in react native web project. When I am getting a token on browser, I am getting error:- Service worker registration failed, error: DOMException: Failed to register a ServiceWorker for scope ('https://localhost:19006/') with script ('https://localhost:19006/firebase-messaging-sw.js'): The script has an unsupported MIME type ('text/html').

Solutions I have found is to create firebase-messaging-sw.js. I have created the one but it is not reading the file. This is my firebase-messaging-sw.js file:-

// 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/7.15.0/firebase-app.js");
// importScripts(
//   "https://www.gstatic.com/firebasejs/7.15.0/firebase-messaging.js"
// );
import * as firebase from "firebase";

const script = document.createElement("script");
script.src = "https://www.gstatic.com/firebasejs/7.15.0/firebase-app.js";
script.async = true;

document.body.appendChild(script);

const script1 = document.createElement("script");
script1.src = "https://www.gstatic.com/firebasejs/7.15.0/firebase-messaging.js";
script1.async = true;

document.body.appendChild(script1);

// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
  apiKey: "*********",
  authDomain: "*********",
  databaseURL: "*********",
  projectId: "*********",
  storageBucket: "*********",
  messagingSenderId: "1772528518",
  appId: "*********",
});

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

Please help me what I am doing wrong here and where should this file be placed in the project structure. I am not able find any guidance or example project for Firebase messaging in React Native Web or Expo Web.

0 Answers
Related