Electron app desktop notification
Posted By : Anil Kumar | 26-Sep-2017
Electron: Electron is a framework for creating native applications with web technologies like node js, angular, java script, HTML, CSS
Electron notifier: First of all install the node notifier in your node package. sudo npm install node notifier.
(i) Create a function for getting the operating system information because node notifier differ for the different operating systems.
var os = require("os");
os.platform();
console.log("OS information " + os.platform() + " " + os.release() + " " + os.hostname());
const notifier = require("node-notifier");
var onError = function(err, response) {
console.error(err, response);
};
(ii) Create notify function wich are called at the starting of application
const updateOnlineStatus = () => {
if (os.platform() == "win32") {
notifier.WindowsBalloon().notify({
message: event.type,
title: "Electron App Network status",
sound: true, //"Bottle",
wait: false
}, onError);
} else {
notifier.notify({
message: event.type,
title: "Electron App Network status",
sound: true, //"Bottle",
wait: false
}, onError);
}
}
(iii) Create the notify function which are invoked when the internet status will changed
if (navigator.onLine) {
if (os.platform() == "win32") {
notifier.WindowsBalloon().notify({
message: "online",
title: "Electron App Network status",
sound: true, //"Bottle",
wait: true
}, onError);
} else {
notifier.notify({
message: "online",
title: "Electron App Network status",
sound: true, //"Bottle",
wait: true
}, onError);
}
} else if (navigator.offline) {
if (os.platform() == "win32") {
notifier.WindowsBalloon().notify({
message: event.type,
title: "Electron App Network status",
sound: true, //"Bottle",
wait: true
}, onError);
} else {
notifier.notify({
message: event.type,
title: "Electron App Network status",
sound: true, //"Bottle",
wait: true
}, onError);
}
}
(iv) Calling the function
window.addEventListener('online', updateOnlineStatus)
window.addEventListener('offline', updateOnlineStatus)
(v) Create a function for handling the click event on notification generated by the electron notifier function
notifier.on('click', function(notifierObject, options) {
// Triggers if `wait: true` and user clicks notification
alert("Callback triggered");
});
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Anil Kumar
Anil is a Web Developer who specializes in creating dynamic and beautiful web projects and has good experience of working in distributed teams.