From 22880fcc2126923ed6d097da464d97080ff6e079 Mon Sep 17 00:00:00 2001 From: Janne Mattila Date: Fri, 30 Jun 2023 14:26:57 +0300 Subject: [PATCH] Update notification handling logic --- src/MyChess.Client/wwwroot/service-worker.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/MyChess.Client/wwwroot/service-worker.js b/src/MyChess.Client/wwwroot/service-worker.js index 1b409c0..dd140e4 100644 --- a/src/MyChess.Client/wwwroot/service-worker.js +++ b/src/MyChess.Client/wwwroot/service-worker.js @@ -127,7 +127,7 @@ self.addEventListener('fetch', (event) => { // be reflected on the first load after each change). //self.addEventListener('fetch', () => { }); -self.addEventListener('push', async (event) => { +self.addEventListener('push', (event) => { console.log(`[My Chess Push] Push event`); if (!self.Notification) { @@ -157,7 +157,7 @@ self.addEventListener('push', async (event) => { } } - await self.registration.showNotification("My Chess", { + const showNotification = self.registration.showNotification("My Chess", { body: notificationData.text, vibrate: [250, 100, 250, 100, 250], badge: '/logo_96x96_monochrome.png', @@ -165,15 +165,17 @@ self.addEventListener('push', async (event) => { data: notificationData.uri }); + event.waitUntil(showNotification); + const clientList = await clients.matchAll({ - type: "all", includeUncontrolled: true + type: "window", includeUncontrolled: true }); if (clientList.length > 0) { let client = clientList[0]; for (let i = 0; i < clientList.length; i++) { - //if (clientList[i].focused) { + if (clientList[i].focused) { client = clientList[i]; - //} + } } client.postMessage(notificationData); }