NotificationAction 对象
🌐 NotificationAction Object
typestring - 操作类型可以是button或selection。selection仅在 Windows 上受支持。textstring(optional)- 给定操作的标签。itemsstring[] (optional) Windows -selection动作type的物品列表。
平台 / 操作支持
🌐 Platform / Action Support
| 操作类型 | 平台支持 | text 的使用 | 默认 text | 限制 |
|---|---|---|---|---|
button | macOS, Windows | 用作按钮的标签 | macOS 上第一个 button 显示“显示”(本地化),否则为空;Windows 使用提供的 text | macOS: 仅第一个用作主要操作;其他显示为附加操作(悬停)。与 hasReply 不兼容(超过第一个被忽略)。 |
selection | Windows | 用作选择菜单提交按钮的标签 | “选择” | 需要一个 items 数组属性指定选项标签。触发 action 事件并带有 (index, selectedIndex),其中 selectedIndex 为选择的选项(>= 0)。在不支持选择操作的平台上会被忽略。 |
macOS 上的按钮支持
🌐 Button support on macOS
为了让 macOS 上的额外通知按钮正常工作,你的应用必须满足以下条件。
🌐 In order for extra notification buttons to work on macOS your app must meet the following criteria.
- 应用已签名
- 应用在
Info.plist中将其NSUserNotificationAlertStyle设置为alert。
如果这些要求中的任何一项未满足,按钮将不会出现。
🌐 If either of these requirements are not met the button won't appear.
Windows 上的选择支持
🌐 Selection support on Windows
要添加一个选择(组合框)样式的操作,请包含一个带有 type: 'selection' 的操作,一个用于提交按钮的 text 标签,以及一个由字符串组成的 items 数组:
🌐 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()
})
当用户激活选择操作时,通知的 action 事件将会被触发,并带有两个参数:actionIndex(操作在 actions 数组中的索引)和 selectedIndex(所选项目的零基索引,如果不可用则为 -1)。在非 Windows 平台上,选择操作会被忽略。
🌐 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.