将通知添加到 Windows 通知中心而不在屏幕上显示
Add notification to Windows notification center without displaying it on screen
我正在构建一个具有自定义通知功能的 Electron 应用程序,其中 html5 div 根据需要在无框架、透明、始终在顶部 window 上出现和消失 window。
效果很好,但是:我仍然喜欢 Windows 通知中心本身,并且希望可以选择在那里查看过去的通知,而不用 [=30= 在屏幕上实际显示它们] api.
我试过:
- 正在查看 HTML5 api 的选项,以寻找不显示通知或立即隐藏 () 通知的选项:运气不好。唯一接近的方法是 .close(),它也会从中心删除通知。
- 查看像 node-notifier 这样的包,但是 none 所使用的通知依赖项提供了一种完全隐藏通知的方法。
虽然我提到了 Node,但我也会接受任何允许我执行此操作的较低级别 API/binding。
提前致谢。
在评论的帮助下,我找到了一种方法来做我想做的事情:
尽管在构建 NodeRT 和使用 electron-rebuild 时遇到了一些麻烦,但这里有一个有效的 PoC:
const { XmlDocument } = require('@nodert-win10-rs4/windows.data.xml.dom');
const {
ToastNotification,
ToastNotificationManager
} = require('@nodert-win10-rs4/windows.ui.notifications');
const localImage = path.join(__dirname, 'icon.png');
const template = `
<toast launch="app-defined-string">
<visual>
<binding template="ToastGeneric">
<image id="1" placement="appLogoOverride" hint-crop="circle" src="${localImage}"/>
</binding>
</visual>
</toast>
`;
const xml = new XmlDocument();
xml.loadXml(template);
const toast = new ToastNotification(xml);
const notifier = ToastNotificationManager.createToastNotifier("com.myapp.testnotif");
toast.suppressPopup = true;
notifier.show(toast);
希望这能帮助遇到相同 highly-specific 问题的人。
除了 PoC 中 显示的内容之外,suppressPopup
密钥确实是解决方案的主要参与者。
当我浏览大量示例和电子代码时,由于我安装的随机 SDK 版本和库,我一直遇到障碍。例如,使用 NodeRT 的包 electron-windows-notifications 无法加载我的 preload.js
,因为我安装的 Windows 10 SDK(内部版本 15063)需要 nodert-win10-cu
而不是默认情况下在大多数解决方案中使用 nodert-wind10-au
.
SDK
Known As
Windows Version
npm Scope
Windows 10, Build 17134
April 2018 Update (Redstone 4)
1803
npmjs.com/org/nodert-win10-rs4
Windows 10, Build 16299
Fall Creators Update (Redstone 3)
1709
npmjs.com/org/nodert-win10-rs3
Windows 10, Build 15063
Creators Update (Redstone 2)
1703
npmjs.com/org/nodert-win10-cu
Windows 10, Build 14393
Anniversary Update (Redstone 1)
1607
npmjs.com/org/nodert-win10-au
Windows 10, Build 10586
Threshold 2
1511
npmjs.com/~nodert-win10
EDIT: I forgot to include the more specific steps I took getting electron-windows-notifications
dependency files on this GitHub issue here. Basically everything from Python
, broken node-gyp
, and missing .winmd
files.
最后,Electron 14+ 和 NodeRT 有一个问题,您需要确保 app.allowRendererProcessReuse = false
并且就像自述文件所说的那样,以确保这是 运行 在 main.js
文件.
希望这对一路走来的人有所帮助,因为我以前从未使用过 Electron,直到今天,并且在其他人的帮助下学到了很多东西。
我正在构建一个具有自定义通知功能的 Electron 应用程序,其中 html5 div 根据需要在无框架、透明、始终在顶部 window 上出现和消失 window。
效果很好,但是:我仍然喜欢 Windows 通知中心本身,并且希望可以选择在那里查看过去的通知,而不用 [=30= 在屏幕上实际显示它们] api.
我试过:
- 正在查看 HTML5 api 的选项,以寻找不显示通知或立即隐藏 () 通知的选项:运气不好。唯一接近的方法是 .close(),它也会从中心删除通知。
- 查看像 node-notifier 这样的包,但是 none 所使用的通知依赖项提供了一种完全隐藏通知的方法。
虽然我提到了 Node,但我也会接受任何允许我执行此操作的较低级别 API/binding。
提前致谢。
在评论
尽管在构建 NodeRT 和使用 electron-rebuild 时遇到了一些麻烦,但这里有一个有效的 PoC:
const { XmlDocument } = require('@nodert-win10-rs4/windows.data.xml.dom');
const {
ToastNotification,
ToastNotificationManager
} = require('@nodert-win10-rs4/windows.ui.notifications');
const localImage = path.join(__dirname, 'icon.png');
const template = `
<toast launch="app-defined-string">
<visual>
<binding template="ToastGeneric">
<image id="1" placement="appLogoOverride" hint-crop="circle" src="${localImage}"/>
</binding>
</visual>
</toast>
`;
const xml = new XmlDocument();
xml.loadXml(template);
const toast = new ToastNotification(xml);
const notifier = ToastNotificationManager.createToastNotifier("com.myapp.testnotif");
toast.suppressPopup = true;
notifier.show(toast);
希望这能帮助遇到相同 highly-specific 问题的人。
除了 PoC 中 suppressPopup
密钥确实是解决方案的主要参与者。
当我浏览大量示例和电子代码时,由于我安装的随机 SDK 版本和库,我一直遇到障碍。例如,使用 NodeRT 的包 electron-windows-notifications 无法加载我的 preload.js
,因为我安装的 Windows 10 SDK(内部版本 15063)需要 nodert-win10-cu
而不是默认情况下在大多数解决方案中使用 nodert-wind10-au
.
SDK | Known As | Windows Version | npm Scope |
---|---|---|---|
Windows 10, Build 17134 | April 2018 Update (Redstone 4) | 1803 | npmjs.com/org/nodert-win10-rs4 |
Windows 10, Build 16299 | Fall Creators Update (Redstone 3) | 1709 | npmjs.com/org/nodert-win10-rs3 |
Windows 10, Build 15063 | Creators Update (Redstone 2) | 1703 | npmjs.com/org/nodert-win10-cu |
Windows 10, Build 14393 | Anniversary Update (Redstone 1) | 1607 | npmjs.com/org/nodert-win10-au |
Windows 10, Build 10586 | Threshold 2 | 1511 | npmjs.com/~nodert-win10 |
EDIT: I forgot to include the more specific steps I took getting
electron-windows-notifications
dependency files on this GitHub issue here. Basically everything fromPython
, brokennode-gyp
, and missing.winmd
files.
最后,Electron 14+ 和 NodeRT 有一个问题,您需要确保 app.allowRendererProcessReuse = false
并且就像自述文件所说的那样,以确保这是 运行 在 main.js
文件.
希望这对一路走来的人有所帮助,因为我以前从未使用过 Electron,直到今天,并且在其他人的帮助下学到了很多东西。