Im working on firebase messaging service to send push notification via django web application to web app on Andriod,IOS and website as well, I managed to get token and run sw ,however i can't running sw and get messages due to importing module issue, I tried all import statements and none has worked with me!!,
when i run the below lines i got this error "Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope':
importScripts('https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging-sw.js');
and when i run these ones i got this error "Cannot use import statement outside a module":
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js";
import { getMessaging } from "https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging-sw.js";
Note I do not have any error when accessing localhost:/firebase-messaging-sw.js and download window open to download firebase-messaging-sw.js file
please find firebase-messaging-sw.js files for your reference:
// import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js";
// import { getMessaging } from "https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging-sw.js";
importScripts('https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging.js');
self.importScripts('https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging-sw.js');
// import { initializeApp } from "./node_modules/@firebase/app";
// import { getMessaging } from "./node_modules/@firebase/messaging/sw";
// import { getMessaging } from "./firebase/@firebase/messaging/sw";
// 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
const BikumApp = firebase.initializeApp({
apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
authDomain: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
databaseURL: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
projectId: 'xxxxxxxxxxxxxxxxxxxxxx',
storageBucket: 'xxxxxxxxxxxxxxxxxxxxxxx',
messagingSenderId: 'xxxxxxxxxxxxxxxxx',
appId: "1xxxxxxxxxxxxxxxxxxxxxxxxxx",
measurementId: 'xxxxxxxxxxxxxxxxxx',
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebae.getMessaging(BikumApp)
console.log(messaging);
// const messaging = getMessaging();
onBackgroundMessage(messaging, (payload) => {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
registration.showNotification(notificationTitle,
notificationOptions);
});