Open In App

Custom Notifications in ElectronJS

ElectronJS

is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime. Electron at its core is a



NodeJS



application having capabilities to interact with the native OS environment such as File System, System Tray etc. For creating interactive native OS applications Electron needs to integrate itself with OS functionalities. One such crucial functionality is Desktop Notifications. All three operating systems have provisions for sending Desktop Notifications. Electron provides the built-in

Notification

Module which allows the application to send Custom Notifications, using the operating systems native notification APIs to display it. This tutorial will demonstrate how to create Custom Desktop Notifications using the Notification Module.

We assume that you are familiar with the prerequisites as covered in the above-mentioned link. For Electron to work,

node

and

npm

need to be pre-installed in the system.

Notifications In Electron:

The Notification Module is a part of the

Main Process

. To import and use the Notification Module in the

Renderer Process

, we can use the

Remote

Module provided by Electron. In this tutorial, we will also be using Electron Remote. For more details on the Remote Module, Refer this

link

.

  • Project Structure:
  • Example:

    We will start by building the basic Electron Application by following the given steps.

    Notification Options:

    Now we will create a Custom Desktop Notification on a

    Windows

    Machine. The

    Notification

    Module provides certain Instance Events, Instance Methods, and Options which are Operating System specific. If a property that is incompatible with the native OS is used, it is Simple Ignored. A detailed list of the same is provided below.

    The Notification Module also provides us with a

    Static

    Method,

    Notification.isSupported()

    . Returns a Boolean value stating whether Notifications are Supported in the Current System or not. In the

    index.html

    file, add the following code before the

    script

    tag.

    The

    Trigger Custom Notification

    Button does not have any functionality associated with it. We will add an

    EventListener

    to the button to trigger the Custom Notification. We will also add

    EventListener

    to the Notification object. Create the

    index.js

    file following the project Structure and perform the following changes.

    Instance events:

    Notification Module also provides two more Instance events which are supported by

    macOS

    only.




    // Emitted when user clicks the reply button from
    // 'hasReply: true' property
    customNotification.addListener('reply', (event, reply) => {
        console.log('Replied String is - ', reply);
    });
     
    // Emitted when the user clicks on any one action
    // defined in the actions:[{}] property
    customNotification.addListener('action', (event, index) => {
        console.log('Index of the action clicked is - ', index);
    });
    
    

    Instance Properties:

    Electron Notification Module also Supports Instance Properties which can be set to the Notification Object. They can be used instead of Options and can also change pre-defined options when triggering Custom Notifications. A detailed list is given below.




    customNotification.title = 'Title has been Changed';
     
    // Supported in macOS
    customNotification.subtitle = 'Subtitle of the Notification'
     
    customNotification.body = 'Body has been changed';
     
    // Supported in macOS
    customNotification.closeButtonText = 'Close Button' 
     
    // Supported in macOS
    customNotification.hasReply = true;
     
    // Supported in macOS
    customNotification.replyPlaceholder = 'Reply Placeholder';
     
    customNotification.silent = false;
     
    // Supported in Linux
    customNotification.urgency = 'low';
     
    // Supported in Windows and Linux OS
    // This is a bug, as described above.
    customNotification.timeoutType= 'default';
    
    
  • Output: Upon adding these Instance properties to the index.js file, we should see the following. All non-compatible instance properties are ignored.

  • Article Tags :