From 0b3f2a7fa05c473720344e8dba3dd27a25cdb12c Mon Sep 17 00:00:00 2001 From: Bruno Windels <brunow@element.io> Date: Wed, 24 Mar 2021 15:25:59 +0100 Subject: [PATCH] improve notification click handling - also here don't use client.url to figure out if a session is open as that doesn't work in FF - use tag to make sure we're dealing with the right type of notif - use findClient function --- src/platform/web/service-worker.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/platform/web/service-worker.js b/src/platform/web/service-worker.js index daef3640..1aba7a96 100644 --- a/src/platform/web/service-worker.js +++ b/src/platform/web/service-worker.js @@ -189,23 +189,24 @@ const NOTIF_TAG_MESSAGES_READ = "messages_read"; const NOTIF_TAG_NEW_MESSAGE = "new_message"; async function openClientFromNotif(event) { - const clientList = await self.clients.matchAll({type: "window"}); + if (event.notification.tag !== NOTIF_TAG_NEW_MESSAGE) { + return; + } 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; + const clientWithSession = await findClient(async client => { + return await sendAndWaitForReply(client, "hasSessionOpen", {sessionId}); + }); + if (clientWithSession) { + console.log("notificationclick: client has session open, showing room there"); + clientWithSession.navigate(roomURL); + if ('focus' in clientWithSession) { + await clientWithSession.focus(); } - } - if (self.clients.openWindow) { + } else if (self.client.openWindow) { + console.log("notificationclick: no client found with session open, opening new window"); await self.clients.openWindow(roomURL); } }