From 0628aac664be4ccce361d319fad201c44d9257fe Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 26 Oct 2023 15:42:21 +0300 Subject: [PATCH] fallback to old notification method, don't spam if old way of creating notification fails, try to use favicon --- .../desktop_notification_utils.js | 14 ++++++++++++-- .../notification_utils/notification_utils.js | 6 +++++- src/services/sw/sw.js | 2 +- src/sw.js | 15 +++++++++------ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/services/desktop_notification_utils/desktop_notification_utils.js b/src/services/desktop_notification_utils/desktop_notification_utils.js index c31a1030..eb58f39b 100644 --- a/src/services/desktop_notification_utils/desktop_notification_utils.js +++ b/src/services/desktop_notification_utils/desktop_notification_utils.js @@ -1,8 +1,18 @@ -import { showDesktopNotification as swDesktopNotification } from '../sw/sw.js' +import { showDesktopNotification as swDesktopNotification, isSWSupported } from '../sw/sw.js' +const state = { failCreateNotif: false } export const showDesktopNotification = (rootState, desktopNotificationOpts) => { if (!('Notification' in window && window.Notification.permission === 'granted')) return if (rootState.statuses.notifications.desktopNotificationSilence) { return } - swDesktopNotification(desktopNotificationOpts) + if (isSWSupported()) { + swDesktopNotification(desktopNotificationOpts) + } else if (!state.failCreateNotif) { + try { + const desktopNotification = new window.Notification(desktopNotificationOpts.title, desktopNotificationOpts) + setTimeout(desktopNotification.close.bind(desktopNotification), 5000) + } catch { + state.failCreateNotif = true + } + } } diff --git a/src/services/notification_utils/notification_utils.js b/src/services/notification_utils/notification_utils.js index 0f8b9b02..ede1cc3d 100644 --- a/src/services/notification_utils/notification_utils.js +++ b/src/services/notification_utils/notification_utils.js @@ -76,8 +76,12 @@ export const unseenNotificationsFromStore = store => filter(filteredNotificationsFromStore(store), ({ seen }) => !seen) export const prepareNotificationObject = (notification, i18n) => { + const nodes = document.querySelectorAll('link[rel="icon"]') + const icon = nodes[0].href + const notifObj = { - tag: notification.id + tag: notification.id, + icon } const status = notification.status const title = notification.from_profile.name diff --git a/src/services/sw/sw.js b/src/services/sw/sw.js index b13c9a1b..3b62bac8 100644 --- a/src/services/sw/sw.js +++ b/src/services/sw/sw.js @@ -10,7 +10,7 @@ function urlBase64ToUint8Array (base64String) { return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0))) } -function isSWSupported () { +export function isSWSupported () { return 'serviceWorker' in navigator } diff --git a/src/sw.js b/src/sw.js index 1889d15f..1b08fe69 100644 --- a/src/sw.js +++ b/src/sw.js @@ -59,16 +59,19 @@ self.addEventListener('message', async (event) => { console.log(event) if (type === 'desktopNotification') { - const { title, body, icon, id } = content - if (state.notificationIds.has(id)) return - state.notificationIds.add(id) - setTimeout(() => state.notificationIds.delete(id), 10000) - self.registration.showNotification('SWTEST: ' + title, { body, icon }) + const { title, ...rest } = content + const { tag } = rest + if (state.notificationIds.has(tag)) return + state.notificationIds.add(tag) + setTimeout(() => state.notificationIds.delete(tag), 10000) + self.registration.showNotification(title, rest) } if (type === 'updateFocus') { state.lastFocused = event.source.id - console.log(state) + + const notifications = await self.registration.getNotifications() + notifications.forEach(n => n.close()) } })