From 13ef686e3681426be52b7440254db0474bbf83c0 Mon Sep 17 00:00:00 2001 From: 10G Meow <10gmeow@gmail.com> Date: Sun, 1 Oct 2023 14:31:00 +0300 Subject: [PATCH] Web notification improvements --- public/sw.js | 22 +++++++++++++ src/App.vue | 30 ++++++++++++++++-- src/components/Chat.vue | 35 +++------------------ src/plugins/notificationAndServiceWorker.js | 20 ++++-------- 4 files changed, 61 insertions(+), 46 deletions(-) diff --git a/public/sw.js b/public/sw.js index e69de29..494d828 100644 --- a/public/sw.js +++ b/public/sw.js @@ -0,0 +1,22 @@ +// Notification click event listener +self.addEventListener("notificationclick", (e) => { + // Close the notification popout + e.notification.close(); + // Get all the Window clients + e.waitUntil( + clients.matchAll({ type: "window" }).then((clientsArr) => { + // If a Window tab matching the targeted URL already exists, focus that; + const hadWindowToFocus = clientsArr.some((windowClient) => + windowClient.url === e.notification.data.url + ? (windowClient.focus(), true) + : false, + ); + + // Otherwise, open a new tab to the applicable URL and focus it. + if (!hadWindowToFocus) + clients + .openWindow(e.notification.data.url) + .then((windowClient) => (windowClient ? windowClient.focus() : null)); + }), + ); +}); \ No newline at end of file diff --git a/src/App.vue b/src/App.vue index 3193757..05e5b22 100644 --- a/src/App.vue +++ b/src/App.vue @@ -31,10 +31,12 @@