跳转至

NotificationAction 对象

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

平台 / 操作支持

操作类型 平台支持 text 的用途 默认 text 限制
button macOS, Windows 用作按钮的标签 在 macOS 上,如果是第一个 button,则为 "Show"(已本地化),否则为空;在 Windows 上使用提供的 text macOS:只有第一个会被用作主要按钮;其他按钮作为附加操作(悬停时)显示。与 hasReply 不兼容(除第一个以外的按钮会被忽略)。
selection Windows 用作选择菜单的提交按钮的标签 "Select" 需要一个 items 数组属性来指定选项标签。携带 (index, selectedIndex) 触发 action 事件,其中 selectedIndex 是被选中的选项(>= 0)。在不支持选择操作的平台上会被忽略。

macOS 上的按钮支持

为了让额外的通知按钮在 macOS 上正常工作,你的应用必须满足以下条件:

  • 应用已签名
  • 应用在其 Info.plist 中将 NSUserNotificationAlertStyle 设置为 alert。

如果任一要求未满足,按钮将不会显示。

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