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

firebase.initializeApp({
    apiKey: "AIzaSyAOMuVBYIo4XUg2iCX-GFNcxHMjfv8NMp0",
      authDomain: "nusa-66d3c.firebaseapp.com",
      databaseURL: "https://nusa-66d3c-default-rtdb.firebaseio.com",
      projectId: "nusa-66d3c",
      storageBucket: "nusa-66d3c.appspot.com",
      messagingSenderId: "206831378979",
      appId: "1:206831378979:web:0ccf9aa08e7595413a8796",
      measurementId: "G-2GXPC4D66T"
});

const messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(function (payload) {
    const promiseChain = clients
        .matchAll({
            type: "window",
            includeUncontrolled: true
        })
        .then(windowClients => {
            for (let i = 0; i < windowClients.length; i++) {
                const windowClient = windowClients[i];
                windowClient.postMessage(payload);
            }
        })
        .then(() => {
            const title = payload.notification.title;
            const options = {
                body: payload.notification.score
              };
            return registration.showNotification(title, options);
        });
    return promiseChain;
});
self.addEventListener('notificationclick', function (event) {
    console.log('notification received: ', event)
});