add badge icon to notifs

This commit is contained in:
Bruno Windels 2021-03-25 00:12:57 +01:00
parent 0b3f2a7fa0
commit 165532be30
2 changed files with 11 additions and 0 deletions

View File

@ -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);
}

View File

@ -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