From 039e0184177497d17281e9b6dc08fb4c46c4ee2d Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 21 Sep 2021 15:23:24 +0200 Subject: [PATCH] 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/ --- ts/WoltLabSuite/Core/Notification/Handler.ts | 21 +++++++++++++++---- .../WoltLabSuite/Core/Notification/Handler.js | 19 +++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) 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(); -- 2.20.1