Skip to content

Instantly share code, notes, and snippets.

@adodado
Last active September 16, 2021 09:41
Show Gist options
  • Save adodado/924c840c6cf23df4c2c6e06ddd4ef451 to your computer and use it in GitHub Desktop.
Save adodado/924c840c6cf23df4c2c6e06ddd4ef451 to your computer and use it in GitHub Desktop.
Building a Cross-Platform Desktop Notification Application with Electron basic functionality Index.js
const { app, Tray, Menu } = require("electron");
const path = require("path");
const iconPath = path.join(__dirname, "icon.png");
var opsys = process.platform;
let trayApp = null;
var status = "enabled"; //Application is in enabled mode at start up
var intervalID;
var interval = 0;
const WindowsBalloon = require("node-notifier").WindowsBalloon;
const notifier = require("node-notifier");
var winNotifier = new WindowsBalloon({
withFallback: false,
customPath: void 0,
});
function sendNotification() {
if (opsys == "win32" || opsys == "win64") {
// Windows specific notification code so that the notifiers filename does not show
winNotifier.notify(
{
title: "BreakTime Buddy",
message: "Test 1",
sound: true,
wait: true, // Wait for User Action against Notification
type: "info", // The notification type : info | warn | error
},
function (err, response) {
console.log(err);
console.log(response);
}
);
} else {
// Any other OS
notifier.notify(
{
title: "BreakTime Buddy",
message: "Test 1",
sound: true,
wait: true, // Wait for User Action against Notification
type: "info", // The notification type : info | warn | error
},
function (err, response) {
console.log(err);
console.log(response);
}
);
}
}
function startNewInterval() {
// Since this will run in specific time intervalls we need a way to manage that and that is below
clearInterval(intervalID);
if (status === "enabled") {
intervalID = setInterval(function () {
sendNotification();
}, interval);
}
}
function getContextMenu() {
// Handles the creation/re-creation of the context menu
let contextMenu = Menu.buildFromTemplate([
{
label: "Enable",
type: "checkbox",
checked: status == "enabled",
click: function () {
if (status === "disabled") {
status = "enabled";
} else {
status = "disabled";
clearInterval(intervalID);
interval=0;
}
trayApp.setContextMenu(getContextMenu());
},
},
{
label: "10 minutes",
type: "checkbox",
checked: interval == (60000 * 10),
click: function () {
if (interval === (60000 * 10)) {
interval = 0;
clearInterval(intervalID);
} else {
interval=(60000 * 10);
startNewInterval();
}
trayApp.setContextMenu(getContextMenu());
},
},
{
label: "Try it out!",
click: function () {
sendNotification();
},
},
{
label: "Exit",
click: function () {
app.exit();
},
},
]);
return contextMenu;
}
app.on("ready", () => {
trayApp = new Tray(iconPath);
var contextMenu = getContextMenu();
trayApp.setToolTip("BreakTime Buddy");
trayApp.setContextMenu(contextMenu);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment