From de08ce8f7b2d446511f12adee3b0f5495d37cf42 Mon Sep 17 00:00:00 2001 From: David Battersby Date: Thu, 15 Aug 2024 18:30:55 +0400 Subject: [PATCH] FIX: allow chat sound when notifications are disabled (#28385) Desktop chat notification sounds have stopped working on most desktop browsers. This is due to Notifications API being disabled when Push Notifications are supported in the browser, which means that we never iterate on the desktopNotificationHandlers and trigger the callback since we return early. --- .../app/lib/desktop-notifications.js | 72 ++++++++++--------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/desktop-notifications.js b/app/assets/javascripts/discourse/app/lib/desktop-notifications.js index c3b6ffc86f4..fdfff4e07f8 100644 --- a/app/assets/javascripts/discourse/app/lib/desktop-notifications.js +++ b/app/assets/javascripts/discourse/app/lib/desktop-notifications.js @@ -135,7 +135,7 @@ function canUserReceiveNotifications(user) { return false; } - if (keyValueStore.getItem("notifications-disabled", "disabled")) { + if (keyValueStore.getItem("notifications-disabled") === "disabled") { return false; } @@ -144,44 +144,46 @@ function canUserReceiveNotifications(user) { // Call-in point from message bus async function onNotification(data, siteSettings, user, appEvents) { - if (!canUserReceiveNotifications(user)) { - return false; - } + const showNotifications = canUserReceiveNotifications(user) && liveEnabled; - if (!liveEnabled) { - return false; - } + if (showNotifications) { + const notificationTitle = + data.translated_title || + I18n.t(i18nKey(data.notification_type), { + site_title: siteSettings.title, + topic: data.topic_title, + username: formatUsername(data.username), + group_name: data.group_name, + }); - const notificationTitle = - data.translated_title || - I18n.t(i18nKey(data.notification_type), { - site_title: siteSettings.title, - topic: data.topic_title, - username: formatUsername(data.username), - group_name: data.group_name, + const notificationIcon = + siteSettings.site_logo_small_url || siteSettings.site_logo_url; + const notificationTag = + "discourse-notification-" + + siteSettings.title + + "-" + + (data.topic_id || 0); + + await requestPermission(); + + const notification = new Notification(notificationTitle, { + body: data.excerpt, + icon: notificationIcon, + tag: notificationTag, }); - const notificationBody = data.excerpt; - - const notificationIcon = - siteSettings.site_logo_small_url || siteSettings.site_logo_url; - - const notificationTag = - "discourse-notification-" + siteSettings.title + "-" + (data.topic_id || 0); - - await requestPermission(); - - // This shows the notification! - const notification = new Notification(notificationTitle, { - body: notificationBody, - icon: notificationIcon, - tag: notificationTag, - }); - notification.onclick = () => { - DiscourseURL.routeTo(data.post_url); - appEvents.trigger("desktop-notification-opened", { url: data.post_url }); - notification.close(); - }; + notification.addEventListener( + "click", + () => { + DiscourseURL.routeTo(data.post_url); + appEvents.trigger("desktop-notification-opened", { + url: data.post_url, + }); + notification.close(); + }, + { once: true } + ); + } desktopNotificationHandlers.forEach((handler) => handler(data, siteSettings, user)