From 0ed2bacd83a05ce3190bfa7124a04a0de6abf9fc Mon Sep 17 00:00:00 2001 From: André Oliveira <37463445+SpecialAro@users.noreply.github.com> Date: Mon, 13 May 2024 22:31:23 +0100 Subject: Fix notifications on all services (#1593) * refactor class * Revert "refactor class" This reverts commit 7e266d7aa4905f236457c44f9ffc37fa8f742b4c. * refactor: change logic * Working logic (but double notifications) * fix: final adjustments --- src/stores/ServicesStore.ts | 3 ++- src/webview/notifications.ts | 59 ++++++++++++++------------------------------ src/webview/recipe.ts | 2 +- 3 files changed, 21 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/stores/ServicesStore.ts b/src/stores/ServicesStore.ts index 9f2cfeca3..58c6b2a87 100644 --- a/src/stores/ServicesStore.ts +++ b/src/stores/ServicesStore.ts @@ -917,12 +917,13 @@ export default class ServicesStore extends TypedStore { if (service.isNotificationEnabled) { let title: string; - options.icon = service.iconUrl; if (this.stores.settings.all.app.privateNotifications === true) { // Remove message data from notification in private mode options.body = ''; + options.icon = service.icon; title = `Notification from ${service.name}`; } else { + options.icon = options.icon || service.icon; options.body = typeof options.body === 'string' ? options.body : ''; title = typeof args[0].title === 'string' ? args[0].title : service.name; diff --git a/src/webview/notifications.ts b/src/webview/notifications.ts index e4401ab6e..835869f5c 100644 --- a/src/webview/notifications.ts +++ b/src/webview/notifications.ts @@ -31,52 +31,29 @@ export class NotificationsHandler { } export const notificationsClassDefinition = `(() => { - class Notification { - static permission = 'granted'; - - constructor(title = '', options = {}) { - this.title = title; - this.options = options; - try { - window.ferdium.displayNotification(title, options) - .then(() => { - if (typeof (this.onClick) === 'function') { - this.onClick(); - } - }); - } catch(error) { - this.options.onClick = null; - window.ferdium.displayNotification(title, options) - .then(() => { - if (typeof (this.onClick) === 'function') { - this.onClick(); - } - }); - } - } - - static requestPermission(cb = null) { - if (!cb) { - return new Promise((resolve) => { - resolve(Notification.permission); - }); - } - - if (typeof (cb) === 'function') { - return cb(Notification.permission); - } - - return Notification.permission; - } +class Notification { + static permission = 'granted'; + + constructor(title = '', options = {}) { + window.ferdium.displayNotification(title, options).then(() => { + // TODO: After several tries, we couldn't find a way to trigger the native notification onclick event. + // This was needed so that user could go to the specific context when clicking on the notification (it only goes to the service now). + // For now, we don't do anything here + }); + } - onNotify(data) { - return data; + static requestPermission(cb) { + if (typeof cb === 'function') { + cb(Notification.permission); } - onClick() {} + return Promise.resolve(Notification.permission); + } - close() {} + onNotify(data) { + return data; } +} window.Notification = Notification; })();`; diff --git a/src/webview/recipe.ts b/src/webview/recipe.ts index a35a99699..ad2215ffd 100644 --- a/src/webview/recipe.ts +++ b/src/webview/recipe.ts @@ -121,7 +121,7 @@ contextBridge.exposeInMainWorld('ferdium', { setDialogTitle: (title: string | null | undefined) => dialogTitleHandler.setDialogTitle(title), displayNotification: (title: string, options: any) => { - notificationsHandler.displayNotification( + return notificationsHandler.displayNotification( title, // The following line is needed so that a proper clone of the "options" object is made. // This line was causing issues with some services. -- cgit v1.2.3-54-g00ecf