diff options
Diffstat (limited to 'frontend/sw.js')
| -rw-r--r-- | frontend/sw.js | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/frontend/sw.js b/frontend/sw.js index 187a66b..dd9a495 100644 --- a/frontend/sw.js +++ b/frontend/sw.js @@ -1,7 +1,47 @@ +const API_URL = "https://backend.laundryweb.altafcreator.com" + self.addEventListener('push', (e) => { - const data = e.data.json(); - console.log(data); - self.registration.showNotification(data.title, { - body: data.body, - }); + 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(); + } }); |
