The FCM doc shows how to handle background messages with Firebase 9 but the examples for Firebase 9 uses import
which as far as I understand is not supported in service workers. The Firebase 8 example correctly uses ImportScripts
.
I have tried using ImportScripts
for Firebase 9:
importScripts('https://www.gstatic.com/firebasejs/9.1.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/9.1.2/firebase-messaging.js');
Both paths exist, but in my app I get an error
The script at 'https://www.gstatic.com/firebasejs/9.1.2/firebase-app.js' failed to load.
Can anyone show some code of a working version of a service worker using Firebase 9 to handle background message?
Note: the doc on how to add Firebase to a web project says
Reserved Hosting URLs are available only with version 8 and earlier. You cannot use version 9 or later with Hosting URLs.
Which would explain why the ImportScripts
doesn't work but how to proceed then?
Use the following imports:
importScripts('https://www.gstatic.com/firebasejs/9.5.0/firebase-app-compat.js')
importScripts('https://www.gstatic.com/firebasejs/9.5.0/firebase-messaging-compat.js')
These are compatibility versions of v9 which should support the other existing code within the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With