Skip to main content

NotificationAction 对象

🌐 NotificationAction Object

  • type 字符串 - 操作类型,可以是 buttonselectionselection 仅在 Windows 上支持。
  • text 字符串(可选)- 给定操作的标签。
  • items string[](可选)Windows - 用于 selection 操作 type 的项目列表。

平台 / 操作支持

🌐 Platform / Action Support

动作类型平台支持text 的使用默认 text限制
buttonmacOS, Windows用作按钮的标签macOS 上首个 button 为“显示”(已本地化),否则为空;Windows 使用提供的 textmacOS:只有第一个用作主要按钮;其他显示为附加动作(悬停)。与 hasReply 不兼容(首个之外的忽略)。
selectionWindows用作选择菜单提交按钮的标签“选择”需要一个 items 数组属性来指定选项标签。触发 action 事件,其中 (index, 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.