Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

desktop_notifications: Detect an unusable Notification constructor.

Prakhar Pratyush85f6f243094f
diff --git a/web/src/desktop_notifications.ts b/web/src/desktop_notifications.ts
index eb2a7215f7f..b1243e2df2d 100644
--- a/web/src/desktop_notifications.ts
+++ b/web/src/desktop_notifications.ts
@@ -64,10 +64,42 @@ export class ElectronBridgeNotification extends EventTarget {
}
}
+// Most mobile browsers expose the Notification interface, but
+// support only persistent notifications, which are created via
+// ServiceWorkerRegistration.showNotification().
+// The Notification() constructor throws a TypeError there, so until
+// we register a service worker, treat them as having no support.
+// See https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API#persistent_and_non-persistent_notifications
+function maybe_construct_notification(
+ title: string,
+ options: NotificationOptions,
+): ElectronBridgeNotification | Notification | undefined {
+ if (NotificationAPI === undefined) {
+ return undefined;
+ }
+ try {
+ return new NotificationAPI(title, options);
+ } catch (error) {
+ if (!(error instanceof TypeError)) {
+ throw error;
+ }
+ NotificationAPI = undefined;
+ return undefined;
+ }
+}
+
if (electron_bridge?.new_notification) {
NotificationAPI = ElectronBridgeNotification;
} else if (window.Notification) {
NotificationAPI = window.Notification;
+ if (Notification.permission !== "granted") {
+ // A notification constructed without permission is never
+ // displayed, which lets us check the constructor without
+ // bothering the user. Once permission has been granted we
+ // cannot make this check, so we treat the browser as supported
+ // until a notification first fails to be constructed.
+ maybe_construct_notification("", {})?.close();
+ }
}
export function create_notification(opts: {
@@ -88,7 +120,10 @@ export function create_notification(opts: {
return;
}
- const notification_object = new NotificationAPI(title, notification_options);
+ const notification_object = maybe_construct_notification(title, notification_options);
+ if (notification_object === undefined) {
+ return;
+ }
notice_memory.set(key, {
obj: notification_object,
data,