- 1 year ago
Electron toast actions on Windows
To create toast notifications with actions using Electron on Windows, you can utilize the node-notifier
module. This module provides cross-platform support for displaying notifications with custom actions, including on Windows.
Here's an example of how you can create toast notifications with actions using Electron and node-notifier
:
- Install the
node-notifier
module by running the following command in your Electron project directory:
shellnpm install node-notifier
- In your Electron code, require the
node-notifier
module:
javascriptconst notifier = require('node-notifier');
- Define a function to handle the action triggered by the notification:
javascriptfunction handleAction() {
console.log('Action triggered');
// Add your custom logic here
}
- Use the
notifier.notify()
method to create the toast notification with actions:
javascriptnotifier.notify(
{
title: 'Notification Title',
message: 'Notification Message',
actions: ['Action 1', 'Action 2'],
reply: true,
closeLabel: 'Close',
timeout: 10,
wait: true,
},
function (error, response, metadata) {
if (metadata.activationValue === 'Action 1') {
handleAction();
} else if (metadata.activationValue === 'Action 2') {
// Handle another action
}
}
);
In this example, we configure the notify()
method with the desired options for the notification:
title
andmessage
represent the title and message content of the notification.actions
is an array of strings representing the available actions.reply
enables the ability to reply to the notification.closeLabel
sets the label for the close button.timeout
defines the duration of the notification in seconds.wait
sets the notification to persist until the user interacts with it.
The callback function receives error
, response
, and metadata
parameters. The metadata.activationValue
property contains the value of the activated action, allowing you to handle different actions accordingly.
By utilizing the node-notifier
module in Electron, you can create toast notifications with actions on Windows. Remember to adjust the code and customize the action handling according to your specific requirements.