NotificationAction オブジェクト
typestring - The type of action, can bebuttonorselection.selectionis only supported on Windows.textstring (任意) - 指定されたアクションのラベル。itemsstring[] (optional) Windows - The list of items for theselectionactiontype.
プラットフォーム / 動作サポート
| アクションタイプ | サポートプラットフォーム | text の用途 | デフォルト text | 制限事項 |
|---|---|---|---|---|
button | macOS, Windows | ボタンのラベル | "Show" on macOS (localized) if first button, otherwise empty; Windows uses provided text | macOS: Only the first one is used as primary; others shown as additional actions (hover). Incompatible with hasReply (beyond first ignored). |
selection | Windows | Used as the label for the submit button for the selection menu | "選択" | Requires an items array property specifying option labels. 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.
上記いずれかの要件が満たされていないと、ボタンは表示されません。
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.