The ToastNotification class allows you to create Windows toast notifications using an XML template and a list of strings. It extends EventEmitter, allowing you to listen for user interactions like activated or dismissed events.
Constructor Options
When instantiating new ToastNotification(options), you can provide the following:
| Option | Type | Description |
|---|
template | string | An XML template string containing placeholders (e.g., %s) for the provided strings. |
strings | string[] | An array of strings that will be sanitized, XML-escaped, and injected into the template placeholders. |
expirationTime | Date | The time at which the notification should expire. |
group | string | A group identifier for the notification. |
tag | string | A tag identifier for the notification. |
appId | string | The Application User Model ID (AUMID). If not provided, it defaults to the value returned by getAppId(). |
Events
activated: Emitted when the user interacts with the notification. The event payload contains the arguments from the ToastActivatedEventArgs.dismissed: Emitted when the notification is dismissed.failed: Emitted if the notification cannot be shown due to system settings (e.g., disabled by Group Policy or App Manifest).
const ToastNotification = require('electron-windows-notifications').ToastNotification;
const notification = new ToastNotification({
template: '<toast><visual><binding template="ToastGeneric">%s</binding></visual></toast>',
strings: ['Hello World!'],
appId: 'YOUR_APP_ID'
});
notification.on('activated', (t, args) => {
console.log('Notification activated with args:', args);
});
notification.show();