Skip to content

Commit

Permalink
desktop: use native electron notifications on desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Jan 19, 2023
1 parent 8385bd4 commit cc39fac
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 17 deletions.
3 changes: 2 additions & 1 deletion apps/web/desktop/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export const EVENTS = {
updateDownloadProgress: "updateDownloadProgress",
updateDownloadCompleted: "updateDownloadCompleted",
updateNotAvailable: "updateNotAvailable",
themeChanged: "themeChanged"
themeChanged: "themeChanged",
notificationClicked: "notificationClicked"
};
24 changes: 24 additions & 0 deletions apps/web/desktop/ipc/actions/bringToFront.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

export default () => {
if (!global.win) return;
global.win.show();
global.win.moveTop();
};
6 changes: 5 additions & 1 deletion apps/web/desktop/ipc/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import open from "./open";
import saveFile from "./saveFile";
import setZoomFactor from "./setZoomFactor";
import setPrivacyMode from "./setPrivacyMode";
import showNotification from "./showNotification";
import bringToFront from "./bringToFront";

const actions = {
changeAppTheme,
Expand All @@ -34,7 +36,9 @@ const actions = {
open,
saveFile,
setZoomFactor,
setPrivacyMode
setPrivacyMode,
showNotification,
bringToFront
};

export function getAction(actionName) {
Expand Down
42 changes: 42 additions & 0 deletions apps/web/desktop/ipc/actions/showNotification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Notification, shell } from "electron";
import { join } from "path";
import { EVENTS } from "../../events";
import { sendMessageToRenderer } from "../utils";
import { platform } from "os";

export default (args) => {
if (!global.win) return;
const notification = new Notification({
...args,
icon: join(
__dirname,
platform() === "win32" ? "app.ico" : "favicon-72x72.png"
)
});
notification.show();
if (args.urgency === "critical") {
shell.beep();
}

notification.addListener("click", () => {
sendMessageToRenderer(EVENTS.notificationClicked, { tag: args.tag });
});
};
24 changes: 24 additions & 0 deletions apps/web/src/commands/bring-to-front.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { invokeCommand } from "./index";

export default function bringToFront() {
invokeCommand("bringToFront");
}
4 changes: 2 additions & 2 deletions apps/web/src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function invokeCommand(type, payload = {}) {
if (isDesktop()) {
window.api.receive("fromMain", (args) => {
console.log(args);
const { type } = args;
AppEventManager.publish(type, args);
const { type, ...other } = args;
AppEventManager.publish(type, other);
});
}
40 changes: 40 additions & 0 deletions apps/web/src/commands/show-notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { EVENTS } from "@notesnook/desktop/events";
import { AppEventManager } from "../common/app-events";
import { invokeCommand } from "./index";

/**
*
* @param {import("electron").NotificationConstructorOptions & { tag: string }} options
* @param {() => void} onClicked
*/
export default function showNotification(options, onClicked) {
invokeCommand("showNotification", options);
const { unsubscribe } = AppEventManager.subscribe(
EVENTS.notificationClicked,
({ tag }) => {
if (tag === options.tag) {
onClicked();
unsubscribe();
}
}
);
}
59 changes: 46 additions & 13 deletions apps/web/src/stores/reminder-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import { showReminderPreviewDialog } from "../common/dialog-controller";
import dayjs from "dayjs";
import Config from "../utils/config";
import { store as notestore } from "./note-store";
import { isTesting } from "../utils/platform";
import { isDesktop, isTesting } from "../utils/platform";
import showNotification from "../commands/show-notification";
import bringToFront from "../commands/bring-to-front";

class ReminderStore extends BaseStore {
reminders = [];
Expand Down Expand Up @@ -81,6 +83,13 @@ async function resetReminders(reminders) {
}
}

/**
*
* @param {string} id
* @param {import("@notesnook/core/collections/reminders").Reminder} reminder
* @param {string} cron
* @returns
*/
function scheduleReminder(id, reminder, cron) {
return TaskScheduler.register(`reminder:${id}`, cron, () => {
if (!Config.get("reminderNotifications", true)) return;
Expand All @@ -90,18 +99,42 @@ function scheduleReminder(id, reminder, cron) {
return;
}

const notification = new Notification(reminder.title, {
body: reminder.description,
vibrate: reminder.priority === "vibrate",
silent: reminder.priority === "silent",
tag: id,
renotify: true,
requireInteraction: true
});

notification.onclick = function () {
showReminderPreviewDialog(reminder);
};
if (isDesktop()) {
showNotification(
{
title: reminder.title,
body: reminder.description,
silent: reminder.priority === "silent",
timeoutType: reminder.priority === "urgent" ? "never" : "default",
urgency:
reminder.priority === "urgent"
? "critical"
: reminder.priority === "vibrate"
? "normal"
: "low",
focusOnClick: true,
tag: id
},
() => {
bringToFront();
showReminderPreviewDialog(reminder);
}
);
} else {
const notification = new Notification(reminder.title, {
body: reminder.description,
vibrate: reminder.priority === "vibrate",
silent: reminder.priority === "silent",
tag: id,
renotify: true,
requireInteraction: true
});

notification.onclick = function () {
window.focus();
showReminderPreviewDialog(reminder);
};
}

store.refresh(false);
});
Expand Down

0 comments on commit cc39fac

Please sign in to comment.