Closed halhwadi closed 2 years ago
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
There are 2 ways to you can import Firebase Messaging in service worker using v9:
importScripts
, but you have to use the compat scripts:
importScripts('https://www.gstatic.com/firebasejs/9.0.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging-compat.js');
Notice the -compat
suffix in the script names. Compat scripts provide the v8 API, so you'd write v8 style code to use them. Therefore, you should follow the Web version 8
snippets in the Firebase devsite.
9.0.0/firebase-app.js
and other non-compat scripts don't work with importScripts
because they are ESMs which are only supported in browser context using <script type="module">
.
import
, and use a bundler like webpack
to build a service worker bundle.
import { initializeApp } from "firebase/app";
import { getMessaging } from "firebase/messaging/sw";
You can find usage examples in the Web version 9
snippets in the Firebase devsite.
And keep in mind you can't import this code directly in service worker, you need to build it to generate a bundle that's compatible with importScripts
(basically means a umd build), and use importScripts
to import the generated file.
Feel free to reopen if you encountered any issues.
@Feiyang1 thank you so muck...I followed your guidelines and got issue solved....
Step 1: Describe your environment
Step 2: Describe the problem:
Im working on firebase messaging service to send push notification via django web application to Andriod,IOS and website as well, I managed to get token ,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
and when i run these ones i got this error "Cannot use import statement outside a module":
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