NotificationAction 对象¶
typestring - 操作类型,可以是button或selection。selection仅在 Windows 上受支持。textstring (可选) - 给定操作的标签。itemsstring[] (可选) Windows -selection操作类型的项目列表。
平台 / 操作支持¶
| 操作类型 | 平台支持 | text 的用途 |
默认 text |
限制 |
|---|---|---|---|---|
button |
macOS、Windows、Linux | 用作按钮的标签 | 如果是第一个 button,在 macOS 上为 "Show"(本地化),否则为空;Windows 使用提供的 text;Linux 在没有 text 时不显示按钮 |
macOS:仅第一个用作主按钮;其他作为附加操作显示(悬停)。与 hasReply 不兼容(第一个之后的会被忽略)。Linux:需要具有 actions 能力的通知服务器。 |
selection |
Windows | 用作选择菜单的提交按钮标签 | "Select" |
需要一个 items 数组属性来指定选项标签。触发 action 事件时带有 (index, selectedIndex),其中 selectedIndex 是所选项(>= 0)。在不支持选择操作的平台上会被忽略。 |
macOS 上的按钮支持¶
要使额外的通知按钮在 macOS 上正常工作,你的应用必须满足以下条件。
- 应用已签名
- 应用在其
Info.plist中将NSUserNotificationAlertStyle设置为alert。
如果这两个要求中有任何一个未满足,按钮将不会显示。
Linux 上的按钮支持¶
仅当桌面环境的通知服务器声明支持 actions 能力 时,按钮才会显示。否则,通知将不带按钮显示。
在 Flatpak 或 Snap 沙箱中,带有按钮的通知还会将其默认操作显示为一个 "Show" 按钮,该按钮会触发 click。
Windows 上的选择操作支持¶
要添加选择(组合框)样式的操作,请包含一个 type: 'selection' 的操作、一个用于提交按钮的 text 标签,以及一个字符串 items 数组:
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 平台上,选择操作会被忽略。
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron