This commit is contained in:
client perkafean
2024-09-28 02:53:22 +00:00
parent 69b3fe4347
commit 3869b4d589
8 changed files with 137 additions and 101 deletions

28
public/service-worker.js Normal file
View File

@@ -0,0 +1,28 @@
// public/firebase-messaging-sw.js
self.addEventListener('push', function(event) {
const data = event.data.json();
const options = {
body: data.body,
icon: 'https://i.pinimg.com/originals/3c/44/67/3c446785968272f79aaac7ace5ded0fe.jpg', // Path to your icon
badge: 'badge.png', // Path to your badge
data: { // Add data to the notification
cafeId: data.cafeId,
transactionId: data.transactionId,
}
};
event.waitUntil(
self.registration.showNotification(data.title, options)
);
});
self.addEventListener('notificationclick', (event) => {
event.notification.close(); // Close the notification
const { cafeId, transactionId } = event.notification.data; // Get the notification data
// Open the URL with the cafeId and transactionId
event.waitUntil(
clients.openWindow(`https://r7l7gs-3000.csb.app/${cafeId}modal=new_transaction&transactionId=${transactionId}`)
);
});

View File

@@ -1,12 +1,9 @@
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker
.register("/service-worker.js")
.then((registration) => {
console.log(
"Service Worker registered with scope:",
registration.scope
);
});
});
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js')
.then((registration) => {
console.log('Service Worker registered with scope:', registration.scope);
})
.catch((error) => {
console.error('Service Worker registration failed:', error);
});
}