Skip to content

Commit

Permalink
Fix notifications on all services (#1593)
Browse files Browse the repository at this point in the history
* refactor class

* Revert "refactor class"

This reverts commit 7e266d7.

* refactor: change logic

* Working logic (but double notifications)

* fix: final adjustments
  • Loading branch information
SpecialAro committed May 13, 2024
1 parent e57527b commit 0ed2bac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 43 deletions.
3 changes: 2 additions & 1 deletion src/stores/ServicesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 18 additions & 41 deletions src/webview/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})();`;
2 changes: 1 addition & 1 deletion src/webview/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0ed2bac

Please sign in to comment.