From 165532be302430c7908d0015badf061e34d94a5a Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 25 Mar 2021 00:12:57 +0100 Subject: [PATCH] add badge icon to notifs --- scripts/build.mjs | 9 +++++++++ src/platform/web/service-worker.js | 2 ++ 2 files changed, 11 insertions(+) diff --git a/scripts/build.mjs b/scripts/build.mjs index ef043135..4d29c9db 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -278,6 +278,13 @@ async function buildServiceWorker(swSource, version, globalHash, assets) { } return newSource; }; + const replaceStringInSource = (name, value) => { + const newSource = swSource.replace(new RegExp(`${name}\\s=\\s"[^"]*"`), `${name} = ${JSON.stringify(value)}`); + if (newSource === swSource) { + throw new Error(`${name} was not found in the service worker source`); + } + return newSource; + }; // write service worker swSource = swSource.replace(`"%%VERSION%%"`, `"${version}"`); @@ -285,6 +292,8 @@ async function buildServiceWorker(swSource, version, globalHash, assets) { swSource = replaceArrayInSource("UNHASHED_PRECACHED_ASSETS", unhashedPreCachedAssets); swSource = replaceArrayInSource("HASHED_PRECACHED_ASSETS", hashedPreCachedAssets); swSource = replaceArrayInSource("HASHED_CACHED_ON_REQUEST_ASSETS", hashedCachedOnRequestAssets); + swSource = replaceStringInSource("NOTIFICATION_BADGE_ICON", assets.resolve("icon.png")); + // service worker should not have a hashed name as it is polled by the browser for updates await assets.writeUnhashed("sw.js", swSource); } diff --git a/src/platform/web/service-worker.js b/src/platform/web/service-worker.js index 1aba7a96..d7d0c34f 100644 --- a/src/platform/web/service-worker.js +++ b/src/platform/web/service-worker.js @@ -20,6 +20,7 @@ const GLOBAL_HASH = "%%GLOBAL_HASH%%"; const UNHASHED_PRECACHED_ASSETS = []; const HASHED_PRECACHED_ASSETS = []; const HASHED_CACHED_ON_REQUEST_ASSETS = []; +const NOTIFICATION_BADGE_ICON = "assets/icon.png"; const unhashedCacheName = `hydrogen-assets-${GLOBAL_HASH}`; const hashedCacheName = `hydrogen-assets`; const mediaThumbnailCacheName = `hydrogen-media-thumbnails-v2`; @@ -261,6 +262,7 @@ async function handlePushNotification(n) { body, data: {sessionId, roomId}, tag: NOTIF_TAG_NEW_MESSAGE + badge: NOTIFICATION_BADGE_ICON }); } else if (n.unread === 0) { // hide the notifs