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");
});
 

About Author

Author Image
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.

Request for Proposal

Name is required

Comment is required

Sending message..