From: Alexander Ebert Date: Tue, 21 Sep 2021 13:23:24 +0000 (+0200) Subject: Skip desktop notifications on Android X-Git-Tag: 5.4.8_dev_1~6 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=039e0184177497d17281e9b6dc08fb4c46c4ee2d;p=GitHub%2FWoltLab%2FWCF.git Skip desktop notifications on Android Notifications are not supported outside of the context of service workers. See https://community.woltlab.com/thread/292374-chrome-android-failed-to-construct-notification-illegal-constructor/ --- diff --git a/ts/WoltLabSuite/Core/Notification/Handler.ts b/ts/WoltLabSuite/Core/Notification/Handler.ts index 64d2fda2c1..9befaf7487 100644 --- a/ts/WoltLabSuite/Core/Notification/Handler.ts +++ b/ts/WoltLabSuite/Core/Notification/Handler.ts @@ -224,10 +224,23 @@ class NotificationHandler { } if (typeof pollData.notification === "object" && typeof pollData.notification.message === "string") { - const notification = new window.Notification(pollData.notification.title, { - body: StringUtil.unescapeHTML(pollData.notification.message).replace(/ /g, "\u202F"), - icon: this.icon, - }); + let notification: Notification; + + try { + notification = new window.Notification(pollData.notification.title, { + body: StringUtil.unescapeHTML(pollData.notification.message).replace(/ /g, "\u202F"), + icon: this.icon, + }); + } catch (e) { + // The `Notification` constructor is not available on Android. + // See https://bugs.chromium.org/p/chromium/issues/detail?id=481856 + if (e.name === "TypeError") { + return; + } + + throw e; + } + notification.onclick = () => { window.focus(); notification.close(); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/Handler.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/Handler.js index 07559ca80a..bc12523103 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/Handler.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/Handler.js @@ -168,10 +168,21 @@ define(["require", "exports", "tslib", "../Ajax", "../Core", "../Event/Handler", return; } if (typeof pollData.notification === "object" && typeof pollData.notification.message === "string") { - const notification = new window.Notification(pollData.notification.title, { - body: StringUtil.unescapeHTML(pollData.notification.message).replace(/ /g, "\u202F"), - icon: this.icon, - }); + let notification; + try { + notification = new window.Notification(pollData.notification.title, { + body: StringUtil.unescapeHTML(pollData.notification.message).replace(/ /g, "\u202F"), + icon: this.icon, + }); + } + catch (e) { + // The `Notification` constructor is not available on Android. + // See https://bugs.chromium.org/p/chromium/issues/detail?id=481856 + if (e.name === "TypeError") { + return; + } + throw e; + } notification.onclick = () => { window.focus(); notification.close();