P
P
Peter2021-10-29 02:59:44
JavaScript
Peter, 2021-10-29 02:59:44

How to run Firebase 9?

Now working with firebase 8 file firebase-messaging-sw.js

importScripts("https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js");

// https://firebase.google.com/docs/web/learn-more

// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({});

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

messaging.setBackgroundMessageHandler(function(payload) {
  
    let options = {
        body: payload.data.body,
        icon: payload.data.icon
    }

    let user = localStorage.getItem('VAFUserId'),
        data = JSON.parse(payload.data['gcm.notification.data']);

    // Search by hash
    if(data.users.find(el => el == user)) {
        return self.registration.showNotification(payload.data.title, options);
    }

});


Everything works fine, respectively, version 9 was released, where they offer to do everything through import

// JavaScript Document
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.1.3/firebase-app.js';
import { getMessaging, onBackgroundMessage } from 'https://www.gstatic.com/firebasejs/9.1.3/firebase-messaging-sw.js';

const firebaseApp = initializeApp({

});

const messaging = getMessaging(firebaseApp);

onBackgroundMessage(messaging, (payload) => {

    let options = {
        body: payload.data.body,
        icon: payload.data.icon
    }

    let user = localStorage.getItem('VAFUserId'),
        data = JSON.parse(payload.data['gcm.notification.data']);

    // Search by hash
    if(data.users.find(el => el == user)) {
        return self.registration.showNotification(payload.data.title, options);
    }

});



When registering a worker, I get the following error: " Uncaught SyntaxError: Cannot use import statement outside a module" which oezhit in a root - not clearly.
The application is not singlepage, but in PHP. Tell me how you can solve this problem or stay on version 8?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question