javascript 使用电子应用程序的通知 API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31606454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Notification API for Electron App
提问by Jo E.
How to implement Notification APIon Electron App?
如何在 Electron App 上实现Notification API?
I tried checking Notification.permission
and it returns granted
我尝试检查Notification.permission
并返回granted
But when I try to run:
但是当我尝试运行时:
new Notification("FOO", {body:"FOOOOOOOOOOOOOOOOOOOOOOOOO"});
new Notification("FOO", {body:"FOOOOOOOOOOOOOOOOOOOOOOOOO"});
nothing happens. Is it even supported?
没发生什么事。它甚至支持吗?
采纳答案by Ana Betts
The notification API doesn't work on Windows, because there is no notification API that works on all versions of Windows (really Win10 is the first version where desktops have a documented API for it, Win8.x had it but it was WinRT-only)
通知 API 在 Windows 上不起作用,因为没有适用于所有版本的 Windows 的通知 API(实际上 Win10 是第一个版本,其中桌面具有记录的 API,Win8.x 有它,但它仅适用于 WinRT )
回答by Wilson Mendes
A good approach is use node-notifierfor notifications, because the package have cross-platform notification support
一个好的方法是使用node-notifier进行通知,因为该包具有跨平台通知支持
回答by MaximeF
Note: Since this is an HTML5 API it is only available in the renderer process.
注意:由于这是一个 HTML5 API,因此仅在渲染器进程中可用。
Example :
例子 :
let myNotification = new Notification('Title', {
body: 'Lorem Ipsum Dolor Sit Amet'
})
myNotification.onclick = () => {
console.log('Notification clicked')
}
Windows
视窗
On Windows 10, notifications "just work".
在 Windows 10 上,通知“正常工作”。
On Windows 8.1 and Windows 8, a shortcut to your app, with a 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.
在 Windows 8.1 和 Windows 8 上,必须在“开始”屏幕上安装带有应用程序用户模型 ID 的应用程序快捷方式。但是请注意,它不需要固定到“开始”屏幕。
On Windows 7, notifications are not supported. You can however send "balloon notifications" using the Tray API.
在 Windows 7 上,不支持通知。但是,您可以使用 Tray API 发送“气球通知”。
Furthermore, the maximum length for the notification body is 250 characters, with the Windows team recommending that notifications should be kept to 200 characters.
此外,通知正文的最大长度为 250 个字符,Windows 团队建议通知应保持为 200 个字符。
More information : https://github.com/electron/electron/blob/master/docs/tutorial/desktop-environment-integration.md#notifications-windows-linux-os-x
更多信息:https: //github.com/electron/electron/blob/master/docs/tutorial/desktop-environment-integration.md#notifications-windows-linux-os-x
回答by aug
Posting an updated answer that Electron has a section in their tutorial for their Notifications and about how to handle Windows
发布一个更新的答案,Electron 在他们的教程中有一个关于通知和如何处理 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.
视窗
- 在 Windows 10 上,必须将带有应用程序用户模型 ID 的应用程序快捷方式安装到“开始”菜单。这在开发过程中可能会有些矫枉过正,因此将 node_modules\electron\dist\electron.exe 添加到您的“开始”菜单也可以解决问题。导航到资源管理器中的文件,右键单击并“固定到开始菜单”。然后,您需要将 app.setAppUserModelId(process.execPath) 行添加到主进程以查看通知。
- 在 Windows 8.1 和 Windows 8 上,必须将带有应用程序用户模型 ID 的应用程序快捷方式安装到“开始”屏幕。但是请注意,它不需要固定到“开始”屏幕。
- 在 Windows 7 上,通知通过自定义实现工作,该实现在视觉上类似于较新系统上的本机实现。Electron 尝试围绕应用程序用户模型 ID 自动化工作。Electron 与安装更新框架 Squirrel 一起使用时,会自动正确设置快捷方式。此外,Electron 会检测到 Squirrel 被使用,并会自动使用正确的值调用 app.setAppUserModelId()。在开发过程中,您可能需要自己调用 app.setAppUserModelId()。
此外,在 Windows 8 中,通知正文的最大长度为 250 个字符,Windows 团队建议通知应保持为 200 个字符。也就是说,该限制已在 Windows 10 中删除,Windows 团队要求开发人员保持合理。尝试向 API 发送大量文本(数千个字符)可能会导致不稳定。
回答by am2505
You should try this
你应该试试这个
const {Notification} = require('electron');
function callNotification(){
let iconAddress = path.join(__dirname, '/icon.png');
const notif={
title: 'Headline',
body: 'Here write your message',
icon: iconAddress
};
new Notification(notif).show();
}
回答by Allan Nienhuis
for window 7, you might try this: 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.mdto communicate between your app and the main electron process which would be responsible for displaying and managing the notifications, unless your main electron process is already responsible for the notification logic.
对于窗口 7,您可以试试这个:https: //github.com/blainesch/electron-notifications- 它生成“桌面”通知作为单独的电子窗口。它看起来很光滑;我现在就要实施。我相信你需要使用类似https://github.com/electron/electron/blob/master/docs/api/ipc-main.md 的东西来在你的应用程序和负责的主电子进程之间进行通信显示和管理通知,除非您的主要电子进程已经负责通知逻辑。