Ir para o conteúdo principal

Objeto NotificationAction

  • type string - The type of action, can be button or selection. selection is only supported on Windows.
  • text string (opcional) - O label para esta ação.
  • items string[] (optional) Windows - The list of items for the selection action type.

Plataforma / Suporte para Action​

Action TypePlataforma com suporteUtilização do texttext padrãoLimitações
buttonmacOS, Windows, LinuxUtilizado como label para o botão"Show" on macOS (localized) if first button, otherwise empty; Windows uses provided text; Linux does not show a button without textmacOS: Only the first one is used as primary; others shown as additional actions (hover). Incompatible with hasReply (beyond first ignored). Linux: Requires a notification server with the actions capability.
selectionWindowsUsed as the label for the submit button for the selection menu"Selecionar"Requires an items array property specifying option labels. Emits the action event with (index, selectedIndex) where selectedIndex is the chosen option (>= 0). Ignored on platforms that do not support selection actions.

Suporte para o botão no macOS​

Para que os botões extra de notificações funcionem no macOS, sua aplicação deverá atender aos seguintes critérios:

  • Aplicação é assinada
  • App has its NSUserNotificationAlertStyle set to alert in the Info.plist.

Se nenhum desses critérios forem atendidos, o botão não irá aparecer.

Button support on Linux​

Buttons are shown only if the desktop's notification server advertises the actions capability. Otherwise the notification is shown without them.

In a Flatpak or Snap sandbox, a notification that has buttons also shows its default action as a "Show" button, which emits click.

Selection support on Windows​

To add a selection (combo box) style action, include an action with type: 'selection', a text label for the submit button, and an items array of strings:

const { Notification, app } = require('electron')

app.whenReady().then(() => {
const items = ['One', 'Two', 'Three']
const n = new Notification({
title: 'Choose an option',
actions: [
{
type: 'selection',
text: 'Apply',
items
}
]
})

n.on('action', (e) => {
console.log(`User triggered action at index: ${e.actionIndex}`)
if (e.selectionIndex > 0) {
console.log(`User chose selection item '${items[e.selectionIndex]}'`)
}
})

n.show()
})

When the user activates the selection action, the notification's action event will be emitted with two parameters: actionIndex (the action's index in the actions array) and selectedIndex (the zero-based index of the chosen item, or -1 if unavailable). On non-Windows platforms selection actions are ignored.