diff options
Diffstat (limited to 'frontend/sw.js')
| -rw-r--r-- | frontend/sw.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/frontend/sw.js b/frontend/sw.js new file mode 100644 index 0000000..dd9a495 --- /dev/null +++ b/frontend/sw.js @@ -0,0 +1,47 @@ +const API_URL = "https://backend.laundryweb.altafcreator.com" + +self.addEventListener('push', (e) => { + console.log(e.data); + const received_data = e.data.json(); + console.log(received_data); + if (received_data.requireInteraction) { + self.registration.showNotification(received_data.title, { + body: received_data.body, + vibrate: [200, 100, 200], + requireInteraction: received_data.requireInteraction, + actions: [ + { + title: "I've collected my laundry!", + action: "collect", + } + ], + data: {timerId: received_data.timerId}, + }); + } else { + self.registration.showNotification(received_data.title, { + body: received_data.body, + vibrate: [200, 100, 200], + }); + } +}); + +self.addEventListener("notificationclick", (event) => { + if (event.action === "collect") { + console.log(event); + timerId = event.notification.data.timerId; + console.log("finishing timer! w/ id "+timerId); + fetch(`${API_URL}/finish`, { + method: "POST", + credentials: "include", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({id: timerId}), + }); + clients.openWindow("/timer/"); + event.notification.close(); + } else { + clients.openWindow("/timer/"); + event.notification.close(); + } +}); |
