fallback to old notification method, don't spam if old way of creating

notification fails, try to use favicon
This commit is contained in:
Henry Jameson 2023-10-26 15:42:21 +03:00
commit 0628aac664
4 changed files with 27 additions and 10 deletions

View file

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

View file

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

View file

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