Each operating system has its own mechanism to display notifications to users. Electron's notification
APIs are cross-platform, but are different for each process type.
If you want to use a renderer process API in the main process or vice-versa, consider using
inter-process communication.
Main process notifications are displayed using Electron's Notification module.
Notification objects created using this module do not appear unless their show() instance
method is called.
Main Process
const{Notification}=require('electron')
constNOTIFICATION_TITLE='Basic Notification'
constNOTIFICATION_BODY='Notification from the Main process'
newNotification({
title:NOTIFICATION_TITLE,
body:NOTIFICATION_BODY
}).show()
Here's a full example that you can open with Electron Fiddle:
For notifications on Windows, your Electron app needs to have a Start Menu shortcut with an
AppUserModelID and a corresponding ToastActivatorCLSID.
Electron attempts to automate the work around the AppUserModelID and ToastActivatorCLSID. When
Electron is used together with Squirrel.Windows (e.g. if you're using electron-winstaller),
shortcuts will automatically be set correctly.
In production, Electron will also detect that Squirrel was used and will automatically call
app.setAppUserModelId() with the correct value. During development, you may have
to call app.setAppUserModelId() yourself.
Notifications in development
To quickly bootstrap notifications during development, adding
node_modules\electron\dist\electron.exe to your Start Menu also does the
trick. Navigate to the file in Explorer, right-click and 'Pin to Start Menu'.
Then, call app.setAppUserModelId(process.execPath) in the main process to see notifications.
Windows also allow for advanced notifications with custom templates, images, and other flexible
elements.
To send those notifications from the main process, you can use the userland module
electron-windows-notifications,
which uses native Node addons to send ToastNotification and TileNotification objects.
While notifications including buttons work with electron-windows-notifications,
handling replies requires the use of
electron-windows-interactive-notifications,
which helps with registering the required COM components and calling your
Electron app with the entered user data.
For notifications on macOS, your application will need to be code-signed in order
for notification events to emit correctly. This requirement stems from the underlying
UNNotification API provided by Apple. Unsigned binaries will emit a failed event
when notification APIs are called.
Notifications are sent using libnotify, which can show notifications on any
desktop environment that follows Desktop Notifications Specification,
including Cinnamon, Enlightenment, Unity, GNOME, and KDE.