Notifications

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.

Usage

Below are two examples showing how to display notifications for each process type.

Show notifications in the main process

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

  1. const { Notification } = require('electron')
  2. const NOTIFICATION_TITLE = 'Basic Notification'
  3. const NOTIFICATION_BODY = 'Notification from the Main process'
  4. new Notification({
  5. title: NOTIFICATION_TITLE,
  6. body: NOTIFICATION_BODY
  7. }).show()

Here’s a full example that you can open with Electron Fiddle:

docs/fiddles/features/notifications/main (27.0.1)Open in Fiddle

  • main.js
  • index.html
  1. const { app, BrowserWindow, Notification } = require('electron')
  2. function createWindow () {
  3. const win = new BrowserWindow({
  4. width: 800,
  5. height: 600
  6. })
  7. win.loadFile('index.html')
  8. }
  9. const NOTIFICATION_TITLE = 'Basic Notification'
  10. const NOTIFICATION_BODY = 'Notification from the Main process'
  11. function showNotification () {
  12. new Notification({ title: NOTIFICATION_TITLE, body: NOTIFICATION_BODY }).show()
  13. }
  14. app.whenReady().then(createWindow).then(showNotification)
  15. app.on('window-all-closed', () => {
  16. if (process.platform !== 'darwin') {
  17. app.quit()
  18. }
  19. })
  20. app.on('activate', () => {
  21. if (BrowserWindow.getAllWindows().length === 0) {
  22. createWindow()
  23. }
  24. })
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Hello World!</title>
  6. <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  7. </head>
  8. <body>
  9. <h1>Hello World!</h1>
  10. <p>After launching this application, you should see the system notification.</p>
  11. </body>
  12. </html>

Show notifications in the renderer process

Notifications can be displayed directly from the renderer process with the web Notifications API.

Renderer Process

  1. const NOTIFICATION_TITLE = 'Title'
  2. const NOTIFICATION_BODY =
  3. 'Notification from the Renderer process. Click to log to console.'
  4. const CLICK_MESSAGE = 'Notification clicked'
  5. new Notification(NOTIFICATION_TITLE, { body: NOTIFICATION_BODY }).onclick =
  6. () => console.log(CLICK_MESSAGE)

Here’s a full example that you can open with Electron Fiddle:

docs/fiddles/features/notifications/renderer (27.0.1)Open in Fiddle

  • main.js
  • index.html
  • renderer.js
  1. const { app, BrowserWindow } = require('electron')
  2. function createWindow () {
  3. const win = new BrowserWindow({
  4. width: 800,
  5. height: 600
  6. })
  7. win.loadFile('index.html')
  8. }
  9. app.whenReady().then(createWindow)
  10. app.on('window-all-closed', () => {
  11. if (process.platform !== 'darwin') {
  12. app.quit()
  13. }
  14. })
  15. app.on('activate', () => {
  16. if (BrowserWindow.getAllWindows().length === 0) {
  17. createWindow()
  18. }
  19. })
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Hello World!</title>
  6. <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  7. </head>
  8. <body>
  9. <h1>Hello World!</h1>
  10. <p>After launching this application, you should see the system notification.</p>
  11. <p id="output">Click it to see the effect in this interface.</p>
  12. <script src="renderer.js"></script>
  13. </body>
  14. </html>
  1. const NOTIFICATION_TITLE = 'Title'
  2. const NOTIFICATION_BODY = 'Notification from the Renderer process. Click to log to console.'
  3. const CLICK_MESSAGE = 'Notification clicked!'
  4. new window.Notification(NOTIFICATION_TITLE, { body: NOTIFICATION_BODY })
  5. .onclick = () => { document.getElementById('output').innerText = CLICK_MESSAGE }

Platform considerations

While code and user experience across operating systems are similar, there are subtle differences.

Windows

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 - 图1Notifications 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.

Use advanced 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.

Query notification state

To detect whether or not you’re allowed to send a notification, use the userland module windows-notification-state.

This module allows you to determine ahead of time whether or not Windows will silently throw the notification away.

macOS

Notifications are straightforward on macOS, but you should be aware of Apple’s Human Interface guidelines regarding notifications.

Note that notifications are limited to 256 bytes in size and will be truncated if you exceed that limit.

Query notification state

To detect whether or not you’re allowed to send a notification, use the userland module macos-notification-state.

This module allows you to detect ahead of time whether or not the notification will be displayed.

Linux

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.