Skip desktop notifications on Android
authorAlexander Ebert <ebert@woltlab.com>
Tue, 21 Sep 2021 13:23:24 +0000 (15:23 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 21 Sep 2021 13:23:24 +0000 (15:23 +0200)
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
wcfsetup/install/files/js/WoltLabSuite/Core/Notification/Handler.js

index 64d2fda2c13e29394c389bdee94f7d62abaaac3f..9befaf7487e1caacf02468060cec37eaacef8fc7 100644 (file)
@@ -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(/&#x202F;/g, "\u202F"),
-        icon: this.icon,
-      });
+      let notification: Notification;
+
+      try {
+        notification = new window.Notification(pollData.notification.title, {
+          body: StringUtil.unescapeHTML(pollData.notification.message).replace(/&#x202F;/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();
index 07559ca80a1b75c5e260d90f125d3134f61b0a91..bc12523103ca9f591cf0cf21980f7c794dc34103 100644 (file)
@@ -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(/&#x202F;/g, "\u202F"),
-                    icon: this.icon,
-                });
+                let notification;
+                try {
+                    notification = new window.Notification(pollData.notification.title, {
+                        body: StringUtil.unescapeHTML(pollData.notification.message).replace(/&#x202F;/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();