当前位置: 首页 > 文档资料 > Electron 中文文档 >

通知 (Windows, Linux, macOS)

优质
小牛编辑
186浏览
2023-12-01

所有三个操作系统都提供了应用程序向用户发送通知的手段。 Electron允许开发者使用 HTML5 Notification API 发送通知,并使用当前运行的操作系统的本地通知 API 来显示它。

注意: 由于这是一个 HTML5 API,它只能在渲染器进程中使用。 如果你想在主进程中显示通知,请查看 Notification 模块.

let myNotification = new Notification('标题', {
  body: '通知正文内容'
})
myNotification.onclick = () => {
  console.log('通知被点击')
}

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

Notifications (Windows, Linux, macOS)

All three operating systems provide means for applications to send notifications to the user. Electron conveniently allows developers to send notifications with the HTML5 Notification API, using the currently running operating system's native notification APIs to display it.

Note: Since this is an HTML5 API it is only available in the renderer process. If you want to show Notifications in the main process please check out the Notification module.

let myNotification = new Notification('Title', {
  body: 'Lorem Ipsum Dolor Sit Amet'
})
myNotification.onclick = () => {
  console.log('Notification clicked')
}

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

Windows

  • 在 Windows 10, 必须被添加您应用程序 应用程序用户模型ID 的快捷方式到开始菜单上。
  • 在 Windows 8.1 和 Windows 8 上,带有 应用程序用户模型ID(Application User Model ID) 的应用程序快捷方式必须被添加到开始屏幕上。 但是请注意,它不需要被固定到开始屏幕。
  • 在 Windows 7 上, 通知通过视觉上类似于较新系统原生的一个自定义的实现来工作。

Electron尝试将应用程序用户模型 ID 的相关工作自动化。 Electron在和安装和更新框架 Squirrel 协同使用的时候,快捷方式将被自动正确的配置好。 更棒的是,Electron 会自动检测 Squirrel 的存在,并且使用正确的值来自动调用app.setAppUserModelId()。 在开发过程中,你可能需要自行调用app.setAppUsesrModelId()(../api/app.md#appsetappusermodelidid-windows)]。

此外,在Windows 8中,通知正文的最大长度为250个字符,Windows团队建议将通知保留为200个字符。 然而,Windows 10中已经删除了这个限制,但是Windows团队要求开发人员合理使用。 尝试将大量文本发送到API(数千个字符) 可能会导致不稳定。

Windows

  • On Windows 10, a shortcut to your app with an Application User Model ID must be installed to the Start Menu. This can be overkill during development, so adding node_moduleselectrondistelectron.exe to your Start Menu also does the trick. Navigate to the file in Explorer, right-click and 'Pin to Start Menu'. You will then need to add the line app.setAppUserModelId(process.execPath) to your main process to see notifications.
  • On Windows 8.1 and Windows 8, a shortcut to your app with an Application User Model ID must be installed to the Start screen. Note, however, that it does not need to be pinned to the Start screen.
  • On Windows 7, notifications work via a custom implementation which visually resembles the native one on newer systems.

Electron attempts to automate the work around the Application User Model ID. When Electron is used together with the installation and update framework Squirrel, shortcuts will automatically be set correctly. Furthermore, Electron will 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.

Furthermore, in Windows 8, the maximum length for the notification body is 250 characters, with the Windows team recommending that notifications should be kept to 200 characters. That said, that limitation has been removed in Windows 10, with the Windows team asking developers to be reasonable. Attempting to send gigantic amounts of text to the API (thousands of characters) might result in instability.

高级通知

Windows 的更高版本允许高级通知,自定义模板,图像和其他灵活元素。 要发送这些通知(来自主进程或渲染器进程),请使用用户区模块 electron-windows-notifications 来用原生节点附件发送 ToastNotificationTileNotification 对象。

虽然包含按钮的通知可以使用 electron-windows-notifications,但处理回复则需要使用electron-windows-interactive-notifications,这有助于注册所需的COM组件,并使用输入的用户数据调用Electron应用程序。

Advanced Notifications

Later versions of Windows allow for advanced notifications, with custom templates, images, and other flexible elements. To send those notifications (from either the main process or the renderer process), use the userland module electron-windows-notifications, which uses native Node addons to send ToastNotification and TileNotification objects.

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.

免打扰模式 / 演示模式

要检测是否允许发送通知,请使用用户区模块 electron-notification-state。

这样,您可以提前确定 Windows 是否会将通知忽略。

Quiet Hours / Presentation Mode

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

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

macOS

MacOS上的通知是最直接的,但你应该注意苹果关于通知的人机接口指南(Apple's Human Interface guidelines regarding notifications).

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

macOS

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

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

高级通知

后来的 macOS 版本允许有一个输入字段的通知,允许用户快速回复通知。 为了通过输入字段发送通知,请使用用户区模块node-mac-notifier。

Advanced Notifications

Later versions of macOS allow for notifications with an input field, allowing the user to quickly reply to a notification. In order to send notifications with an input field, use the userland module node-mac-notifier.

勿扰 / 会话状态

要检测是否允许发送通知,请使用用户区模块 electron-notification-state。

这样可以提前检测是否显示通知。

Do not disturb / Session State

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

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

Linux

通知是通过libnotify发送的,libnotify可以在任何实现了桌面通知规范(Desktop Notifications Specification)的桌面环境中发送通知,包括Cinnamon、Enlightenment、Unity、GNOME、KDE

Linux

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