Skip to main content

通知

¥Notifications

每个操作系统都有自己的向用户显示通知的机制。Electron 的通知 API 是跨平台的,但对于每种进程类型都是不同的。

¥Each operating system has its own mechanism to display notifications to users. Electron's notification APIs are cross-platform, but are different for each process type.

如果你想在主进程中使用渲染器进程 API,反之亦然,请考虑使用 进程间通信

¥If you want to use a renderer process API in the main process or vice-versa, consider using inter-process communication.

用法

¥Usage

以下两个示例展示了如何显示每种进程类型的通知。

¥Below are two examples showing how to display notifications for each process type.

在主进程中显示通知

¥Show notifications in the main process

主进程通知使用 Electron 的 通知模块 显示。使用此模块创建的通知对象不会出现,除非调用它们的 show() 实例方法。

¥Main process notifications are displayed using Electron's Notification module. Notification objects created using this module do not appear unless their show() instance method is called.

Main Process
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 打开的完整示例:

¥Here's a full example that you can open with Electron Fiddle:

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

可以使用 网络通知 API 直接从渲染器进程显示通知。

¥Notifications can be displayed directly from the renderer process with the web Notifications API.

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 打开的完整示例:

¥Here's a full example that you can open with Electron Fiddle:

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

虽然跨操作系统的代码和用户体验相似,但存在细微的差异。

¥While code and user experience across operating systems are similar, there are subtle differences.

Windows

对于 Windows 上的通知,你的 Electron 应用需要有一个带有 应用用户模型 ID 和相应的 ToastActivatorCLSID 的开始菜单快捷方式。

¥For notifications on Windows, your Electron app needs to have a Start Menu shortcut with an AppUserModelID and a corresponding ToastActivatorCLSID.

Electron 尝试自动化围绕 AppUserModelID 和 ToastActivatorCLSID 的工作。当 Electron 与 Squirrel.Windows 一起使用时(例如,如果你使用 electro-winstaller),快捷键将自动正确设置.

¥Electron attempts to automate the work around the AppUserModelID and ToastActivatorCLSID. When Electron is used together with Squirrel.Windows (e.g. if you're using electron-winstaller), shortcuts will automatically be set correctly.

在生产中,Electron 还会检测到 Squirrel 被使用,并会自动使用正确的值调用 app.setAppUserModelId()。在开发过程中,你可能需要自己调用 app.setAppUserModelId()

¥In production, Electron will also detect that Squirrel was used and will automatically call app.setAppUserModelId() with the correct value. During development, you may have to call app.setAppUserModelId() yourself.

开发中的通知

为了在开发过程中快速引导通知,将 node_modules\electron\dist\electron.exe 添加到“开始”菜单也可以达到目的。在资源管理器中导航到该文件,右键单击并选择 '固定到开始菜单'。然后,在主进程中调用 app.setAppUserModelId(process.execPath) 来查看通知。

¥To quickly bootstrap notifications during development, adding node_modules\electron\dist\electron.exe to your Start Menu also does the trick. Navigate to the file in Explorer, right-click and 'Pin to Start Menu'. Then, call app.setAppUserModelId(process.execPath) in the main process to see notifications.

使用高级通知

¥Use advanced notifications

Windows 还允许使用自定义模板、图片和其他灵活元素进行高级通知。

¥Windows also allow for advanced notifications with custom templates, images, and other flexible elements.

要从主进程发送这些通知,你可以使用用户层模块 electron-windows-notifications,该模块使用原生 Node 插件发送 ToastNotificationTileNotification 对象。

¥To send those notifications from the main process, you can use the userland module electron-windows-notifications, which uses native Node addons to send ToastNotification and TileNotification objects.

虽然包括按钮在内的通知可与 electron-windows-notifications 配合使用,但处理响应需要使用 electron-windows-interactive-notifications,这有助于注册所需的 COM 组件并使用输入的用户数据调用你的 Electron 应用。

¥While notifications including buttons work with electron-windows-notifications, handling replies requires the use of electron-windows-interactive-notifications, which helps with registering the required COM components and calling your Electron app with the entered user data.

查询通知状态

¥Query notification state

要检测是否允许你发送通知,请使用用户层模块 windows-notification-state

¥To detect whether or not you're allowed to send a notification, use the userland module windows-notification-state.

该模块允许你提前确定 Windows 是否会默默地丢弃通知。

¥This module allows you to determine ahead of time whether or not Windows will silently throw the notification away.

苹果系统

¥macOS

通知在 macOS 上很简单,但你应该注意 Apple 关于通知的人机界面指南

¥Notifications are straightforward on macOS, but you should be aware of Apple's Human Interface guidelines regarding notifications.

请注意,通知的大小限制为 256 字节,如果超过该限制,通知将被截断。

¥Note that notifications are limited to 256 bytes in size and will be truncated if you exceed that limit.

查询通知状态

¥Query notification state

要检测是否允许你发送通知,请使用用户层模块 macos-notification-state

¥To detect whether or not you're allowed to send a notification, use the userland module macos-notification-state.

该模块允许你提前检测是否会显示通知。

¥This module allows you to detect ahead of time whether or not the notification will be displayed.

Linux

通知使用 libnotify 发送,可以在 桌面通知规范 之后的任何桌面环境上显示通知,包括 Cinnamon、Enlightenment、Unity、GNOME 和 KDE。

¥Notifications are sent using libnotify, which can show notifications on any desktop environment that follows Desktop Notifications Specification, including Cinnamon, Enlightenment, Unity, GNOME, and KDE.