the code below adds a listener to a firestore collection and sends a push notification when the web app is in the foreground. Is it possible to do the same in the background, perhaps, via service worker? I tried to import the firebase module to the service worker but I did not succeed
db.collection("favorites").onSnapshot(function(snapshot) {
var response =snapshot.docChanges();
response.forEach(function(change) {
if (change.type === "added") {
var bodyPaint="New favorites: ";
}
if (change.type === "modified") {
var bodyPaint="Modified favorites: ";
}
if (change.type === "removed") {
var bodyPaint="Removed favorites: ";
}
navigator.serviceWorker.ready.then(function(registration) {
const title = 'LA BUENOS AIRES';
const options = {
body: bodyPaint,
icon: 'images/icon.png',
badge: 'images/badge.png'
};
registration.showNotification(title, options);
});
});
});