felixrieseberg / electron-windows-notifications

:zap: Send native Windows WinRT notifications from Electron
MIT License
263 stars 40 forks source link
electron notifications windows

electron-windows-notifications

Create native Windows toast and tile notifications for Windows 8, 8.1, and 10 using native bindings to WinRT (using NodeRT).

npm install --save electron-windows-notifications

From Electron v14 on, you'll have to require NodeRT modules in the main process. See https://github.com/NodeRT/NodeRT/issues/158 for details.

Usage

For more samples, check out the samples folder.

:memo: Want to respond to interactive notifications (with input fields, for instance)? You can send them with this module - and respond to them with electron-windows-interactive-notifications!

:memo: Want to check if you should send a notification?, accounting for the user notification state and quiet hours? Check out electron-notification-state.

ToastNotification

The constructor for the ToastNotification class accepts an options object with the following properties:

const appId = 'electron-windows-notifications'
const {ToastNotification} = require('electron-windows-notifications')

let notification = new ToastNotification({
    appId: appId,
    template: `<toast><visual><binding template="ToastText01"><text id="1">%s</text></binding></visual></toast>`,
    strings: ['Hi!']
})

notification.on('activated', () => console.log('Activated!'))
notification.show()

TileNotification

Tile notifications update the app's primary or secondary tiles. They require that the app is running inside the UWP model, which is possible on Windows 10 Anniversary Update and later. For more information, check out electron-windows-store. The constructor for the TileNotification class accepts an options object with the following properties:

const appId = 'electron-windows-notifications'
const {ToastNotification} = require('electron-windows-notifications')

let notification = new ToastNotification({
    appId: appId,
    template: `<toast><visual><binding template="ToastText01"><text id="1">%s</text></binding></visual></toast>`,
    strings: ['Hi!']
})

notification.on('activated', () => console.log('Activated!'))
notification.show()

Hints

appUserModelId

The appUserModelId identifies application in Windows. In Electron, you can set it at runtime using the app.setAppUserModelId() method. If you don't pass it, this module will assume that it's a property on Electron's main thread. Unless running inside a UWP container, you probably want to pass it.

Mysterious Failures & XML Escaping

Microsoft follows the XML spec to the letter - and XML has escaping rules you might not be familiar with. If you're adding strings and properties to your template, consider just using the strings property in the constructor - it will automatically properly escape all input.

String replacement

Inside the module, util.format is used. It'll format the template in a printf-like format. The template is expected to hold zero or more placeholders. Each placeholder is replaced with the converted value from its corresponding argument. If the placeholder does not have a corresponding argument, the placeholder is not replaced. Supported placeholders are:

License

MIT. Please see LICENSE for details.