From 63773f99ee77fd22dd5b7d176618c2dc54039137 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Thu, 10 Jun 2021 17:01:26 +0200 Subject: [PATCH] Add Notification workaround for Chrome on Android Closes: https://todo.sr.ht/~emersion/gamja/78 --- components/app.js | 55 +++++++++++++++++++++++++++----------------- components/buffer.js | 17 +++++++++++++- 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/components/app.js b/components/app.js index fbe0b83..bb807e3 100644 --- a/components/app.js +++ b/components/app.js @@ -88,6 +88,21 @@ function debounce(f, delay) { }; } +function showNotification(title, options) { + if (!window.Notification || Notification.permission !== "granted") { + return new EventTarget(); + } + + // This can still fail due to: + // https://bugs.chromium.org/p/chromium/issues/detail?id=481856 + try { + return new Notification(title, options); + } catch (err) { + console.error("Failed to show notification: ", err); + return new EventTarget(); + } +} + export default class App extends Component { state = { connectParams: { @@ -345,12 +360,12 @@ export default class App extends Component { msgUnread = Unread.MESSAGE; } - if (msgUnread == Unread.HIGHLIGHT && window.Notification && Notification.permission === "granted" && !isDelivered && !irc.parseCTCP(msg)) { + if (msgUnread == Unread.HIGHLIGHT && !isDelivered && !irc.parseCTCP(msg)) { var title = "New " + kind + " from " + msg.prefix.name; if (client.isChannel(bufName)) { title += " in " + bufName; } - var notif = new Notification(title, { + var notif = showNotification(title, { body: stripANSI(text), requireInteraction: true, }); @@ -364,25 +379,23 @@ export default class App extends Component { msgUnread = Unread.HIGHLIGHT; var channel = msg.params[1]; - if (window.Notification && Notification.permission === "granted") { - var notif = new Notification("Invitation to " + channel, { - body: msg.prefix.name + " has invited you to " + channel, - requireInteraction: true, - actions: [{ - action: "accept", - title: "Accept", - }], - }); - notif.addEventListener("click", (event) => { - if (event.action === "accept") { - this.setReceipt(bufName, ReceiptType.READ, msg); - this.open(channel, serverID); - } else { - // TODO: scroll to message - this.switchBuffer({ server: serverID, name: bufName }); - } - }); - } + var notif = new Notification("Invitation to " + channel, { + body: msg.prefix.name + " has invited you to " + channel, + requireInteraction: true, + actions: [{ + action: "accept", + title: "Accept", + }], + }); + notif.addEventListener("click", (event) => { + if (event.action === "accept") { + this.setReceipt(bufName, ReceiptType.READ, msg); + this.open(channel, serverID); + } else { + // TODO: scroll to message + this.switchBuffer({ server: serverID, name: bufName }); + } + }); } if (!client.isMyNick(msg.prefix.name) && (msg.command != "PART" && msg.comand != "QUIT")) { diff --git a/components/buffer.js b/components/buffer.js index f80352e..1e51f00 100644 --- a/components/buffer.js +++ b/components/buffer.js @@ -306,6 +306,21 @@ class FoldGroup extends Component { } } +// Workaround for https://bugs.chromium.org/p/chromium/issues/detail?id=481856 +var notificationsSupported = false; +if (window.Notification) { + notificationsSupported = true; + if (Notification.permission === "default") { + try { + new Notification(""); + } catch (err) { + if (err.name === "TypeError") { + notificationsSupported = false; + } + } + } +} + class NotificationNagger extends Component { state = { nag: false }; @@ -318,7 +333,7 @@ class NotificationNagger extends Component { } shouldNag() { - return window.Notification && Notification.permission !== "granted" && Notification.permission !== "denied"; + return notificationsSupported && Notification.permission === "default"; } handleClick(event) {