diff options
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/desktop_notification_utils/desktop_notification_utils.js | 17 | ||||
| -rw-r--r-- | src/services/favicon_service/favicon_service.js | 5 | ||||
| -rw-r--r-- | src/services/notification_utils/notification_utils.js | 11 | ||||
| -rw-r--r-- | src/services/sw/sw.js (renamed from src/services/push/push.js) | 33 |
4 files changed, 52 insertions, 14 deletions
diff --git a/src/services/desktop_notification_utils/desktop_notification_utils.js b/src/services/desktop_notification_utils/desktop_notification_utils.js index b84a1f75..eb58f39b 100644 --- a/src/services/desktop_notification_utils/desktop_notification_utils.js +++ b/src/services/desktop_notification_utils/desktop_notification_utils.js @@ -1,9 +1,18 @@ +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 } - const desktopNotification = new window.Notification(desktopNotificationOpts.title, desktopNotificationOpts) - // Chrome is known for not closing notifications automatically - // according to MDN, anyway. - setTimeout(desktopNotification.close.bind(desktopNotification), 5000) + 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/favicon_service/favicon_service.js b/src/services/favicon_service/favicon_service.js index 7e19629d..df603bb4 100644 --- a/src/services/favicon_service/favicon_service.js +++ b/src/services/favicon_service/favicon_service.js @@ -55,10 +55,13 @@ const createFaviconService = () => { }) } + const getOriginalFavicons = () => [...favicons] + return { initFaviconService, clearFaviconBadge, - drawFaviconBadge + drawFaviconBadge, + getOriginalFavicons } } diff --git a/src/services/notification_utils/notification_utils.js b/src/services/notification_utils/notification_utils.js index 815e792d..fbd5c014 100644 --- a/src/services/notification_utils/notification_utils.js +++ b/src/services/notification_utils/notification_utils.js @@ -1,6 +1,9 @@ import { filter, sortBy, includes } from 'lodash' import { muteWordHits } from '../status_parser/status_parser.js' import { showDesktopNotification } from '../desktop_notification_utils/desktop_notification_utils.js' +import FaviconService from 'src/services/favicon_service/favicon_service.js' + +let cachedBadgeUrl = null export const notificationsFromStore = store => store.state.statuses.notifications.data @@ -76,8 +79,14 @@ export const unseenNotificationsFromStore = store => filter(filteredNotificationsFromStore(store), ({ seen }) => !seen) export const prepareNotificationObject = (notification, i18n) => { + if (cachedBadgeUrl === null) { + const favicon = FaviconService.getOriginalFavicons()[0] + cachedBadgeUrl = favicon.favcanvas.toDataURL() + } + const notifObj = { - tag: notification.id + tag: notification.id, + badge: cachedBadgeUrl } const status = notification.status const title = notification.from_profile.name diff --git a/src/services/push/push.js b/src/services/sw/sw.js index 1787ac36..2875d36e 100644 --- a/src/services/push/push.js +++ b/src/services/sw/sw.js @@ -10,8 +10,12 @@ function urlBase64ToUint8Array (base64String) { return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0))) } +export function isSWSupported () { + return 'serviceWorker' in navigator +} + function isPushSupported () { - return 'serviceWorker' in navigator && 'PushManager' in window + return 'PushManager' in window } function getOrCreateServiceWorker () { @@ -39,7 +43,7 @@ function unsubscribePush (registration) { } function deleteSubscriptionFromBackEnd (token) { - return window.fetch('/api/v1/push/subscription/', { + return fetch('/api/v1/push/subscription/', { method: 'DELETE', headers: { 'Content-Type': 'application/json', @@ -78,6 +82,24 @@ function sendSubscriptionToBackEnd (subscription, token, notificationVisibility) return responseData }) } +export async function initServiceWorker () { + if (!isSWSupported()) return + await getOrCreateServiceWorker() + navigator.serviceWorker.addEventListener('message', (event) => { + console.log('SW MESSAGE', event) + // TODO actually act upon click (open drawer on mobile, open chat/thread etc) + }) +} + +export async function showDesktopNotification (content) { + const { active: sw } = await window.navigator.serviceWorker.getRegistration() + sw.postMessage({ type: 'desktopNotification', content }) +} + +export async function updateFocus () { + const { active: sw } = await window.navigator.serviceWorker.getRegistration() + sw.postMessage({ type: 'updateFocus' }) +} export function registerPushNotifications (isEnabled, vapidPublicKey, token, notificationVisibility) { if (isPushSupported()) { @@ -98,13 +120,8 @@ export function unregisterPushNotifications (token) { }) .then(([registration, unsubResult]) => { if (!unsubResult) { - console.warn('Push subscription cancellation wasn\'t successful, killing SW anyway...') + console.warn('Push subscription cancellation wasn\'t successful') } - return registration.unregister().then((result) => { - if (!result) { - console.warn('Failed to kill SW') - } - }) }) ]).catch((e) => console.warn(`Failed to disable Web Push Notifications: ${e.message}`)) } |
