Notification
创建操作系统桌面通知
进程: 主进程
如果您想从渲染进程显示通知,应该使用 Web Notifications API
类: Notification
创建操作系统桌面通知
进程: 主进程
Notification 是一个 EventEmitter。
它创建一个新的 Notification,其原生属性由 options 设置。
Electron 的内置类不能在用户代码中被继承。有关更多信息,请参阅 FAQ。
静态方法
Notification 类具有以下静态方法
Notification.isSupported()
返回 boolean - 当前系统是否支持桌面通知
new Notification([options])
实例事件
使用 new Notification 创建的对象会发出以下事件
某些事件仅在特定操作系统上可用,并已相应标记。
事件: 'show'
返回
eventEvent
当向用户显示通知时发出。请注意,此事件可以多次触发,因为可以通过 show() 方法多次显示通知。
事件: 'click'
返回
eventEvent
当用户点击通知时发出。
事件: 'close'
返回
eventEvent
当用户手动干预关闭通知时发出。
并非在所有关闭通知的情况下都会保证发出此事件。
在 Windows 上,close 事件可以通过三种方式发出:使用 notification.close() 进行编程关闭、用户关闭通知或通过系统超时。如果通知在发出初始 close 事件后位于操作中心,则调用 notification.close() 将从操作中心删除通知,但 close 事件将不会再次发出。
事件: 'reply' macOS
返回
eventEventreplystring - 用户在内联回复字段中输入的字符串。
当用户点击具有 hasReply: true 的通知上的“回复”按钮时发出。
事件: 'action' macOS
返回
eventEventindexnumber - 激活的操作的索引。
事件: 'failed' Windows
返回
eventEventerrorstring - 在执行show()方法期间遇到的错误。
在创建和显示本机通知时遇到错误时发出。
实例方法
使用 new Notification() 构造函数创建的对象具有以下实例方法
notification.show()
立即向用户显示通知。与 Web Notification API 不同,实例化 new Notification() 不会立即向用户显示它。相反,您需要在操作系统显示它之前调用此方法。
如果通知之前已显示过,此方法将取消显示先前显示的通知,并创建一个具有相同属性的新通知。
notification.close()
关闭通知。
在 Windows 上,在屏幕上可见时调用 notification.close() 将关闭通知并将其从操作中心删除。如果调用 notification.close() 时通知不再屏幕上可见,则调用 notification.close() 将尝试将其从操作中心删除。
实例属性
notification.title
一个 string 属性,表示通知的标题。
notification.subtitle
一个 string 属性,表示通知的副标题。
notification.body
一个 string 属性,表示通知的正文。
notification.replyPlaceholder
一个 string 属性,表示通知的回复占位符。
notification.sound
一个 string 属性,表示通知的声音。
notification.closeButtonText
一个 string 属性,表示通知的关闭按钮文本。
notification.silent
一个 boolean 属性,表示通知是否静音。
notification.hasReply
一个 boolean 属性,表示通知是否具有回复操作。
notification.urgency Linux
一个 string 属性,表示通知的紧急程度。可以是 'normal'、'critical' 或 'low'。
默认值为 'low' - 请参阅 NotifyUrgency 以获取更多信息。
notification.timeoutType Linux Windows
一个 string 属性,表示通知的超时持续时间类型。可以是 'default' 或 'never'。
如果将 timeoutType 设置为 'never',则通知将永远不会过期。它将保持打开状态,直到由调用 API 或用户关闭。
notification.actions
一个 NotificationAction[] 属性,表示通知的操作。
notification.toastXml Windows
一个 string 属性,表示通知的自定义 Toast XML。
播放声音
在 macOS 上,您可以指定要在显示通知时播放的声音的名称。除了自定义声音文件之外,可以使用所有默认声音(在“系统偏好设置”>“声音”下)。请确保声音文件已复制到应用程序包下(例如,YourApp.app/Contents/Resources),或以下位置之一
~/Library/Sounds/Library/Sounds/Network/Library/Sounds/System/Library/Sounds
请参阅 NSSound 文档以获取更多信息。