通知¶
每个操作系统都有自己向用户显示通知的机制。Electron 的通知 API 是跨平台的,但每种进程类型的 API 有所不同。
如果你想在主进程中使用渲染进程 API,反之亦然,请考虑使用进程间通信。
使用¶
下面是两个示例,展示如何针对每种进程类型显示通知。
在主进程中显示通知¶
主进程通知使用 Electron 的 Notification 模块 显示。使用此模块创建的 Notification 对象必须调用其 show() 实例方法才会显示。
const { Notification } = require('electron')
const NOTIFICATION_TITLE = 'Basic Notification'
const NOTIFICATION_BODY = 'Notification from the Main process'
new Notification({
title: NOTIFICATION_TITLE,
body: NOTIFICATION_BODY
}).show()
以下是一个完整示例,你可以使用 Electron Fiddle 打开:
```fiddle docs/fiddles/features/notifications/main const { app, BrowserWindow, Notification } = require('electron/main')
function createWindow () { const win = new BrowserWindow({ width: 800, height: 600 })
win.loadFile('index.html') }
const NOTIFICATION_TITLE = 'Basic Notification' const NOTIFICATION_BODY = 'Notification from the Main process'
function showNotification () { new Notification({ title: NOTIFICATION_TITLE, body: NOTIFICATION_BODY }).show() }
app.whenReady().then(createWindow).then(showNotification)
app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() } })
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } })
### 在渲染进程中显示通知 {#show-notifications-in-the-renderer-process}
通知可以直接通过 [web Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API) 从渲染进程显示。
```js title='Renderer Process'
const NOTIFICATION_TITLE = 'Title'
const NOTIFICATION_BODY = 'Notification from the Renderer process. Click to log to console.'
const CLICK_MESSAGE = 'Notification clicked'
new Notification(NOTIFICATION_TITLE, { body: NOTIFICATION_BODY }).onclick = () => console.log(CLICK_MESSAGE)
以下是一个完整示例,你可以使用 Electron Fiddle 打开:
```fiddle docs/fiddles/features/notifications/renderer|focus=renderer.js const NOTIFICATION_TITLE = 'Title' const NOTIFICATION_BODY = 'Notification from the Renderer process. Click to log to console.' const CLICK_MESSAGE = 'Notification clicked!'
new window.Notification(NOTIFICATION_TITLE, { body: NOTIFICATION_BODY }) .onclick = () => { document.getElementById('output').innerText = CLICK_MESSAGE }
## 平台注意事项 {#platform-considerations}
虽然不同操作系统上的代码和用户体验相似,但仍存在细微差别。
### Windows {#windows}
在 Windows 上显示通知时,你的 Electron 应用需要有一个带有 [AppUserModelID][app-user-model-id] 和相应 [ToastActivatorCLSID][toast-activator-clsid] 的“开始”菜单快捷方式。
Electron 会尝试自动完成与 AppUserModelID 和 ToastActivatorCLSID 相关的工作。当 Electron 与 Squirrel.Windows 一起使用时(例如,如果你使用 electron-winstaller),[快捷方式将自动正确设置][squirrel-events]。
在生产环境中,Electron 还会检测到已使用 Squirrel,并自动使用正确的值调用 `app.setAppUserModelId()`。在开发过程中,你可能需要自行调用 [`app.setAppUserModelId()`][set-app-user-model-id]。
:::info 开发中的通知
在开发过程中,若要快速启动通知,也可以将 `node_modules\electron\dist\electron.exe` 添加到“开始”菜单,这样同样有效。在文件资源管理器中找到该文件,右键单击并选择“固定到‘开始’菜单”。然后在主进程中调用 `app.setAppUserModelId(process.execPath)` 即可看到通知。
:::
#### 使用高级通知 {#use-advanced-notifications}
Windows 支持使用自定义模板、图片、输入和操作按钮的高级通知。
若要在 Windows 上从主进程发送高级通知,请使用 `Notification` 构造函数的 [`toastXml`][notification-toast-xml] 选项。此属性接受符合 Microsoft [Toast XML Schema][ms-toast-schema] 的 XML 字符串,并取代其他通知选项。
```js title='Main Process'
const { Notification } = require('electron')
const xml = `
<toast>
<visual>
<binding template="ToastGeneric">
<text>Notification Title</text>
<text>Notification body text</text>
</binding>
</visual>
<actions>
<action content="Confirm" arguments="action=confirm" />
<action content="Dismiss" arguments="action=dismiss" />
</actions>
</toast>`
const notification = new Notification({ toastXml: xml })
notification.show()
在 Windows 上使用 Toast XML 时的重要注意事项:
- AppUserModelID:在显示通知之前,你必须调用
app.setAppUserModelId()。Windows 会使用此 ID 将 toast 通知与你的应用在“通知中心”中关联起来。 - 图片路径:当在
<image src="..." />中包含本地图片时,路径必须是绝对路径,并格式化为file:///URL(例如,使用url.pathToFileURL(imagePath).toString())。 - XML 转义:放置在 XML 元素或属性中的动态字符串必须进行 XML 转义(
&、<、>、"、'),以防止 XML 解析失败。 - 错误处理:如果 XML 字符串格式错误或不符合 Windows toast 架构,通知将不会显示,并且会在
Notification实例上触发failed事件。 - 处理操作按钮:操作按钮指定
arguments属性。点击后,Electron 会发出带有操作索引的action事件。如需进行深层链接或自定义协议激活,请在<action>元素上指定activationType="protocol"。
查询通知状态¶
要检测是否允许发送通知,请使用用户态模块 windows-notification-state。
该模块允许你提前确定 Windows 是否会静默丢弃通知。
macOS¶
对于 macOS 上的通知,你的应用程序需要进行代码签名,通知事件才能正确发出。这一要求源于 Apple 提供的底层 UNNotification API。未签名的二进制文件在调用通知 API 时会发出 failed 事件。
此外,你应该了解 Apple 关于通知的《人机界面指南》。
请注意,通知的大小限制为 256 字节,如果超出该限制,通知将被截断。
查询通知状态¶
要检测是否允许发送通知,请使用用户态模块 macos-notification-state。
该模块允许你提前检测通知是否会被显示。
Linux¶
通知通过 libnotify 发送,它可以在任何遵循 桌面通知规范 的桌面环境中显示通知,包括 Cinnamon、Enlightenment、Unity、GNOME 和 KDE。
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron