带有节点通知器的电子显示 windows 10 通知 [英] Electron with node-notifier display windows 10 notification
问题描述
我正在尝试制作一个简单的应用程序,该应用程序应在单击按钮时显示通知.问题是通知没有显示,但console.logs 显示.通知是否应该适用于开发模式?(意味着只运行 electron .
,我不必构建和安装应用程序)
I'm trying to make a simple app that should display notification when button is clicked. The problem is that the notification does not show, but console.logs are showing. Should the notification work on development mode? (meaning just running electron .
, and I don't have to build and install the app)
Windows 操作系统:
- 版本:Windows 10 家庭版
- 版本:1709
- 构建:16299.98
- 注意:在
System->Notification & 下启用 Toast(横幅、操作中心)操作
代码:
// main.js
const { app, BrowserWindow, ipcMain, Notification } = require("electron");
const path = require("path");
const url = require("url");
let win;
function createWindow() {
// Create the browser window.
win = new BrowserWindow({ width: 800, height: 600 });
// and load the index.html of the app.
win.loadURL(
url.format({
pathname: path.join(__dirname, "index.html"),
protocol: "file:",
slashes: true
})
);
// Open the DevTools.
// win.webContents.openDevTools()
// Emitted when the window is closed.
win.on("closed", () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
});
}
const appId = "elite-notifier";
app.setAppUserModelId(appId);
app.on("ready", createWindow);
console.log("notifying");
ipcMain.on("notify", () => {
console.log("notified");
const WindowsToaster = require("node-notifier").WindowsToaster;
const notifier = new WindowsToaster({
withFallback: false
});
notifier.notify(
{
title: "My awesome title",
message: "Hello from node, Mr. User!",
sound: true, // Only Notification Center or Windows Toasters
wait: false // Wait with callback, until user action is taken against notification
},
function(err, response) {
// Response is response from notification
console.log("responded...");
}
);
});
// index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Notifier!</h1>
<button type="button" id="notify">Click here to trigger a notification!</button>
<script type="text/javascript">
const { ipcRenderer } = require('electron');
const button = document.getElementById('notify');
console.log('BUTTON: ', button)
button.addEventListener('click', function(event) {
console.log('clicked...');
ipcRenderer.send('notify')
});
</script>
</body>
</html>
推荐答案
我已经搞定了,感谢这里的所有人:) https://github.com/mikaelbr/node-notifier/issues/144#issuecomment-319324058
I've got it working now, thanks to all the people here :) https://github.com/mikaelbr/node-notifier/issues/144#issuecomment-319324058
根据 anthonyraymond
的评论,您需要将您的应用 INSTALLED
安装在带有 appId 的 Windows 机器中.您可以像这样在 package.json
中配置 appId
.
Based on anthonyraymond
's comment, you need to have your app INSTALLED
in your windows machine with an appId. You can configure appId
in your package.json
like this.
{
"name": "myapp",
"version": "1.0.0",
"description": "test",
"main": "main.js",
"build": {
"appId": "com.myapp.id"
}
}
appId
不需要那种 java/android
格式,我的应用只要有 elite-notifier 的
appId
代码>.
The appId
does not need to have that java/android
format, my app just have an appId
of elite-notifier
.
那么就可以在调用notifier的notify
函数时传入appId
了.
Then you can pass the appId
when calling the notify
function of notifier.
notifier.notify(
{
appName: "com.myapp.id", <-- yes, the key here is appName :)
title: "Hello",
message: "Hello world!",
wait: true
},
function(err, response) {
// Response is response from notification
console.log("responded...");
}
);
安装后,这甚至可以在开发模式下工作(通过运行 electron .
命令),前提是您不会更改 appId
安装后的应用程序,因为安装的应用程序与应用程序的开发版本不匹配.
After installation, This will work even on development mode (by running electron .
command) provided that you'll not change the appId
of your app after installation since there will be a mismatch on the installed one and the development version of the app.
这篇关于带有节点通知器的电子显示 windows 10 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!