NotificationAction 对象
typestring - The type of action, can bebuttonorselection. 仅在 Windows 上支持selection。text类型:String(可选) - 给定操作的标签。itemsstring[] (optional) Windows - The list of items for theselectionactiontype.
平台 / 操作 支持
| 操作类型 | 平台支持 | text 参数 | text 参数默认值 | 局限性 |
|---|---|---|---|---|
button | macOS, Windows, Linux | button 显示的内容 | "Show" on macOS (localized) if first button, otherwise empty; Windows uses provided text; Linux does not show a button without text | macOS: Only the first one is used as primary; others shown as additional actions (hover). Incompatible with hasReply (beyond first ignored). Linux:需要一个带有 actions 功能的通知服务器。 |
selection | Windows | 用于选择菜单提交按钮的标签 | "选择" | 需要一个 items 数组属性指定选项标签。 Emits the action event with (index, selectedIndex) where selectedIndex is the chosen option (>= 0). Ignored on platforms that do not support selection actions. |
MacOS系统上的按钮支持
为了在 macOS 上使用额外的通知按钮, 您的应用程序必须符合以下标准。
- 应用程序已签名
- App has its
NSUserNotificationAlertStyleset toalertin theInfo.plist.
这些要求如果有一个不满足,按钮就不会显示。
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.