参考资料
Notification API 文档 👉🏻 https://www.electronjs.org/docs/latest/api/notification
Notification API 简介
在 Electron 中,Notification 是一个用于向用户发送桌面通知的 API。它可以在应用程序需要向用户提供实时信息、提醒或者更新时使用。Notification API 的工作方式与浏览器中的 Notification API 类似,但是 Electron 的 Notification API 可以提供更丰富的功能以及操作系统级别的定制化。通过使用该 API,开发者可以为他们的应用程序生成本地通知,这些通知可以包含图像、文本和操作按钮等内容,可以通过操作按钮来执行应用程序的自定义操作。
demo | 显示、关闭 notification
源码:
const {Notification, // 从 electron 模块中导入 Notification 模块app} = require('electron');app.whenReady().then(() => {// 新建一个通知实例const notification = new Notification({title: '通知的标题',body: '通知的正文内容',icon: './avatar.jpeg' // 通知的图标});// 5 秒后显示通知setTimeout(() => {console.log('show notification')notification.show()// 5 秒后隐藏通知setTimeout(() => {console.log('close notification')notification.close()}, 5000)}, 5000)});
这段代码导入了 Electron 中的 Notification 模块和 app 模块,使用了 app.whenReady 方法来等待应用程序准备就绪,以便正确创建通知。
在 whenReady 方法的回调函数中,创建了一个 Notification 实例并传入标题、正文内容和图标的配置。在 5 秒后,调用 notification.show() 方法来显示通知,并在 5 秒后调用 notification.close() 方法来关闭通知。其中,setTimeout 方法用于延迟操作的实现。
常见配置
Electron 的 Notification API 提供了许多配置选项,以便定制通知的外观和行为。以下是一些常用的配置选项:
title(字符串):通知的标题。这是创建通知时必需的参数。subtitle(字符串,仅适用于 macOS):通知的副标题。body(字符串):通知的正文内容。这通常是需要传达给用户的主要信息。icon(字符串或 NativeImage):通知的图标。可以是图像文件的路径或 NativeImage 对象。silent(布尔值):设置为true以禁用通知发出的声音。urgency(字符串,仅适用于 Linux):设置通知的优先级。可选值有 ‘low’, ‘normal’, ‘critical’。actions(对象数组,仅适用于 macOS):为通知添加一组按钮。每个按钮需要一个标签和一个唯一标识符。closeButtonText(字符串,仅适用于 macOS):自定义通知的关闭按钮文本。timeoutType(字符串,仅适用于 Windows):设置通知的超时类型。可选值有 ‘default’(根据系统设置自动隐藏)或 ‘never’(永不超时)。
const {Notification,app,BrowserWindow} = require('electron')let winfunction showNotification() {const notification = new Notification({title: 'title area……',subtitle: 'subtitle area……',body: 'body area……',icon: './avatar.jpeg',silent: true,urgency: 'critical',actions: [{type: 'button',text: 'actions[0] text...',id: 'view-action'},{type: 'button',text: 'actions[1] text...',id: 'ignore-action'}],closeButtonText: 'closeButtonText',});notification.show();}app.whenReady().then(() => {win = new BrowserWindow({webPreferences: {nodeIntegration: true,contextIsolation: false}})win.loadFile('./index.html')showNotification();})
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Helo World</title></head><body><h1>Helo World</h1></body></html>


注意,不是所有的配置选项都适用于所有平台。在跨平台开发时,请确保测试每个平台的通知行为。
常见事件
Electron 的 Notification 对象提供了一些事件,以便在通知的生命周期中进行响应。以下是一些常用的 Notification 事件:
show:当通知显示时触发。click:当用户点击通知时触发。close:当通知关闭时触发。action:当用户点击通知上的操作按钮时触发。这个事件在 macOS 中是可用的。reply:当用户在通知上进行回复时触发。这个事件在 macOS 和 Windows 上是可用的。
function showNotification() {const notification = new Notification({title: 'title area……',subtitle: 'subtitle area……',body: 'body area……',icon: './avatar.jpeg',silent: true,urgency: 'critical',actions: [{type: 'button',text: 'actions[0] text...',id: 'view-action'},{type: 'button',text: 'actions[1] text...',id: 'ignore-action'}],closeButtonText: 'closeButtonText',});// 添加事件监听器notification.on('show', () => {console.log('Notification shown');});notification.on('click', () => {console.log('Notification clicked');});notification.on('close', () => {console.log('Notification closed');});notification.on('action', (event, index) => {console.log(`Notification action clicked: index ${index}`);});notification.on('reply', (event, reply) => {console.log(`User replied: ${reply}`);});notification.show();}app.whenReady().then(() => {win = new BrowserWindow({webPreferences: {nodeIntegration: true,contextIsolation: false}});win.loadFile('./index.html');showNotification();});
上述代码添加了 show, click, close, action, 和 reply 事件的监听器,并在事件触发时在控制台输出相应的日志。请注意,action 和 reply 事件仅在特定平台上可用。
我们可以根据自己的需求修改事件处理函数,例如在点击通知时执行某个操作或在用户回复时执行其他操作
