Estoy trabajando en el servicio de mensajería de firebase para enviar notificaciones automáticas a través de la aplicación web django a la aplicación web en Andriod, IOS y el sitio web también, logré obtener token y ejecutar sw, sin embargo, no puedo ejecutar sw y recibir mensajes debido a un problema de módulo de importación ¡¡Probé todas las declaraciones de importación y ninguna me ha funcionado!!,
cuando ejecuto las líneas a continuación, aparece este error "DOMException no detectada: no se pudo ejecutar 'importScripts' en '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');y cuando ejecuto estos, aparece este error "No se puede usar la declaración de importación fuera de un módulo":
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";Tenga en cuenta que no tengo ningún error al acceder a localhost:/firebase-messaging-sw.js y la ventana de descarga se abre para descargar el archivo firebase-messaging-sw.js
busque los archivos firebase-messaging-sw.js para su referencia:
// 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); });