Files
bodyshop/client/public/firebase-messaging-sw.js

50 lines
1.5 KiB
JavaScript

importScripts("https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js");
importScripts(
"https://www.gstatic.com/firebasejs/7.14.2/firebase-messaging.js"
);
firebase.initializeApp({
apiKey: "AIzaSyDV9MsSHZmpLtjoaTK_ObvjFaJ-nMSd2KA",
authDomain: "bodyshop-dev-b1cb6.firebaseapp.com",
databaseURL: "https://bodyshop-dev-b1cb6.firebaseio.com",
projectId: "bodyshop-dev-b1cb6",
storageBucket: "bodyshop-dev-b1cb6.appspot.com",
messagingSenderId: "922785209028",
appId: "1:922785209028:web:96e9df15401eee5d784791",
measurementId: "G-2D5378VCHE",
});
const messaging = firebase.messaging();
//Handles Background Messages
messaging.setBackgroundMessageHandler(function (payload) {
console.log("backgroundMessageHandler", 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(() => {
return registration.showNotification(JSON.stringify(payload));
});
return promiseChain;
});
//Handles the notification getting clicked.
self.addEventListener("notificationclick", function (event) {
// do what you want
// ...
console.log("SW notificationclick", event);
});
// self.addEventListener("message", (message) => {
// console.log("Push from SW", message);
// // registration.showNotification("Push from SW" + JSON.stringify(message.data));
// });