From 9538e7be7ac05aa4bd2e45cf33f6572dda07e02d Mon Sep 17 00:00:00 2001 From: ClaraCrazy Date: Sun, 14 Dec 2025 14:18:37 +0100 Subject: [PATCH] Fix notifications --- .../web/public/javascripts/notifications.js | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/infrastructure/web/public/javascripts/notifications.js b/infrastructure/web/public/javascripts/notifications.js index fb075c1..7aa800e 100644 --- a/infrastructure/web/public/javascripts/notifications.js +++ b/infrastructure/web/public/javascripts/notifications.js @@ -2,56 +2,56 @@ /* eslint no-undef: 0 */ function showNewMailsNotification(address, reloadPage) { - // We want the page to be reloaded. But then when clicking the notification, it can not find the tab and will open a new one. + // We want the page to be reloaded. But then when clicking the notification, it can not find the tab and will open a new one. - const notification = new Notification(address, { - body: 'You have new messages', - icon: '/images/logo.png', - tag: '48hr-email-replace-notification', - renotify: true - }) - notification.addEventListener('click', event => { - // TODO: does not work after reloading the page, see #1 - event.preventDefault() - }) + const notification = new Notification(address, { + body: 'You have new messages', + icon: '/images/logo.png', + tag: '48hr-email-replace-notification', + renotify: true + }) + notification.addEventListener('click', event => { + event.preventDefault() + window.focus() + }) - if (reloadPage) { - location.reload() - } + if (reloadPage) { + location.reload() + } } function enableNewMessageNotifications(address, reloadPage) { - enableNotifications() - const socket = io() - socket.emit('sign in', address) + enableNotifications() + const socket = io() + socket.emit('sign in', address) - socket.on('reconnect', () => { - socket.emit('sign in', address) - }) - socket.on('new emails', () => { - showNewMailsNotification(address, reloadPage) - }) + socket.on('reconnect', () => { + socket.emit('sign in', address) + }) + socket.on('new emails', () => { + showNewMailsNotification(address, reloadPage) + }) } function enableNotifications() { - // Let's check if the browser supports notifications - if (!('Notification' in window)) { - return false - } + // Let's check if the browser supports notifications + if (!('Notification' in window)) { + return false + } - // Let's check whether notification permissions have already been granted - if (Notification.permission === 'granted') { - return true - } + // Let's check whether notification permissions have already been granted + if (Notification.permission === 'granted') { + return true + } - // Otherwise, we need to ask the user for permission - if (Notification.permission !== 'denied') { - Notification.requestPermission(permission => { - // If the user accepts, let's create a notification - return permission === 'granted' - }) - } + // Otherwise, we need to ask the user for permission + if (Notification.permission !== 'denied') { + Notification.requestPermission(permission => { + // If the user accepts, let's create a notification + return permission === 'granted' + }) + } - // Finally, if the user has denied notifications and you - // want to be respectful there is no need to bother them any more. + // Finally, if the user has denied notifications and you + // want to be respectful there is no need to bother them any more. }