为 Electron 应用程序使用通知 API

Using Notification API for Electron App

如何在 Electron App 上实现 Notification API

我试过检查 Notification.permission 并且 returns granted

但是当我尝试 运行:

new Notification("FOO", {body:"FOOOOOOOOOOOOOOOOOOOOOOOOO"});

没有任何反应。甚至支持吗?

通知API不适用于Windows,因为没有通知API适用于Windows的所有版本(实际上Win10是第一个桌面有记录的 API 的版本,Win8.x 有它但它是 WinRT-only)

一个好的方法是使用 node-notifier 通知,因为包有 cross-platform 通知支持

注意:由于这是一个 HTML5 API,它仅在渲染器进程中可用。

示例:

let myNotification = new Notification('Title', {
  body: 'Lorem Ipsum Dolor Sit Amet'
})

myNotification.onclick = () => {
  console.log('Notification clicked')
}

Windows

在 Windows 10,通知 "just work"。

在 Windows 8.1 和 Windows 8 上,必须将具有应用程序用户模型 ID 的应用程序快捷方式安装到“开始”屏幕。但请注意,它不需要固定到“开始”屏幕。

在Windows 7 上,不支持通知。但是,您可以使用托盘 API.

发送 "balloon notifications"

此外,通知正文的最大长度为 250 个字符,Windows 团队建议通知应保持在 200 个字符以内。

更多信息:https://github.com/electron/electron/blob/master/docs/tutorial/desktop-environment-integration.md#notifications-windows-linux-os-x

对于 window 7,你可以试试这个:https://github.com/blainesch/electron-notifications - it generates 'desktop' notifications as separate electron windows. It looks pretty slick; I'm about to implement it now. I believe you'll need to use something like https://github.com/electron/electron/blob/master/docs/api/ipc-main.md 在你的应用程序和负责显示和管理通知的主要电子进程之间进行通信,除非你的主要电子进程是已经负责通知逻辑。

发布更新的答案,Electron 在他们的教程中有一个部分用于 Notifications and about how to handle Windows

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_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'. 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.

HTML5NotificationAPI仅在渲染器进程中可用。

如果您想使用来自主进程的通知,您必须 require Electron Notification API。 API 与 standard/HTML5 版本不同。

const {Notification} = require('electron');

new Notification({
    title: 'Headline',
    body: 'Here write your message'
}).show();

使用

 const notification = new window.Notification(
        'LO SIENTO OCURRIO EL ERROR:',
        {
          body: error,
        }
      );

      notification.onclick = () => console.log('Clicked');
      notification.onclose = () => console.log('Closed');