From 725098f262c61f9b0f80f88e047641d48508547e Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 18 Mar 2021 20:46:11 +0100 Subject: [PATCH] open client when clicking notification --- src/platform/web/service-worker.template.js | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/platform/web/service-worker.template.js b/src/platform/web/service-worker.template.js index f46150f6..0e28ac7a 100644 --- a/src/platform/web/service-worker.template.js +++ b/src/platform/web/service-worker.template.js @@ -185,6 +185,33 @@ self.addEventListener('message', (event) => { } }); +async function openClientFromNotif(event) { + const clientList = await self.clients.matchAll({type: "window"}); + const {sessionId, roomId} = event.notification.data; + const sessionHash = `#/session/${sessionId}`; + const roomHash = `${sessionHash}/room/${roomId}`; + const roomURL = `/${roomHash}`; + for (let i = 0; i < clientList.length; i++) { + const client = clientList[i]; + const url = new URL(client.url, baseURL); + if (url.hash.startsWith(sessionHash)) { + client.navigate(roomURL); + if ('focus' in client) { + await client.focus(); + } + return; + } + } + if (self.clients.openWindow) { + await self.clients.openWindow(roomURL); + } +} + +self.addEventListener('notificationclick', event => { + event.notification.close(); + event.waitUntil(openClientFromNotif(event)); +}); + self.addEventListener('push', event => { const n = event.data.json(); console.log("got a push message", n);