带节点通知程序的电子显示 windows 10 条通知
Electron with node-notifier display windows 10 notification
我正在尝试制作一个简单的应用程序,它应该在单击按钮时显示通知。问题是通知没有显示,但是 console.logs 正在显示。通知应该在开发模式下工作吗? (意思是 运行 electron .
,我不需要构建和安装应用程序)
Windows OS:
- 版次:Windows10家
- 版本:1709
- 内部版本:16299.98
- 注意:在
System->Notification & Actions
下启用 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>
你不需要使用IPC并从主进程发送通知,这是supported from the renderer process using the HTML5 notification API。
let myNotification = new Notification('Title', {
body: 'Lorem Ipsum Dolor Sit Amet'
})
myNotification.onclick = () => {
console.log('Notification clicked')
}
我现在已经开始工作了,感谢这里的所有人:) https://github.com/mikaelbr/node-notifier/issues/144#issuecomment-319324058
根据 anthonyraymond
的评论,您的应用程序 INSTALLED
需要在您的 windows 机器中使用 appId。您可以像这样在 package.json
中配置 appId
。
{
"name": "myapp",
"version": "1.0.0",
"description": "test",
"main": "main.js",
"build": {
"appId": "com.myapp.id"
}
}
appId
不需要 java/android
格式,我的应用程序只有 appId
elite-notifier
。
那么在调用notifier的notify
函数时就可以传入appId
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...");
}
);
安装后,如果您不更改 appId
安装后你的应用程序,因为安装的版本和应用程序的开发版本不匹配。
我也试过很多东西,但有时行不通。
终于找到方法了。这不仅适用于 "npm run dev",而且适用于构建的包。
- 在渲染器进程中使用通知。
在 package.json
注册应用程序 ID
"build": {
"appId": "my app id",
...
在主进程 (https://electronjs.org/docs/api/app#appsetappusermodelidid-windows) 调用 app.setAppUserModelId("my app id")
这里的app id可以是任意类型的字符串
你可以做到这一点,即使在 windows 10.
上也对我有用
- 在package.json
"build": {
"appId": "your.custom.app.id",
}
- 在main.js
app.setAppUserModelId("your.custom.app.id");
此错误不需要任何配置,您只需转到您的设置并启用通知因为在某些情况下,您只是在计算机上关闭了通知。
System Setting ->Notification & Actions
然后点击开关打开通知(问题解决)
我正在尝试制作一个简单的应用程序,它应该在单击按钮时显示通知。问题是通知没有显示,但是 console.logs 正在显示。通知应该在开发模式下工作吗? (意思是 运行 electron .
,我不需要构建和安装应用程序)
Windows OS:
- 版次:Windows10家
- 版本:1709
- 内部版本:16299.98
- 注意:在
System->Notification & Actions
下启用 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>
你不需要使用IPC并从主进程发送通知,这是supported from the renderer process using the HTML5 notification API。
let myNotification = new Notification('Title', {
body: 'Lorem Ipsum Dolor Sit Amet'
})
myNotification.onclick = () => {
console.log('Notification clicked')
}
我现在已经开始工作了,感谢这里的所有人:) https://github.com/mikaelbr/node-notifier/issues/144#issuecomment-319324058
根据 anthonyraymond
的评论,您的应用程序 INSTALLED
需要在您的 windows 机器中使用 appId。您可以像这样在 package.json
中配置 appId
。
{
"name": "myapp",
"version": "1.0.0",
"description": "test",
"main": "main.js",
"build": {
"appId": "com.myapp.id"
}
}
appId
不需要 java/android
格式,我的应用程序只有 appId
elite-notifier
。
那么在调用notifier的notify
函数时就可以传入appId
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...");
}
);
安装后,如果您不更改 appId
安装后你的应用程序,因为安装的版本和应用程序的开发版本不匹配。
我也试过很多东西,但有时行不通。
终于找到方法了。这不仅适用于 "npm run dev",而且适用于构建的包。
- 在渲染器进程中使用通知。
在 package.json
注册应用程序 ID"build": { "appId": "my app id", ...
在主进程 (https://electronjs.org/docs/api/app#appsetappusermodelidid-windows) 调用 app.setAppUserModelId("my app id")
这里的app id可以是任意类型的字符串
你可以做到这一点,即使在 windows 10.
上也对我有用- 在package.json
"build": {
"appId": "your.custom.app.id",
}
- 在main.js
app.setAppUserModelId("your.custom.app.id");
此错误不需要任何配置,您只需转到您的设置并启用通知因为在某些情况下,您只是在计算机上关闭了通知。
System Setting ->Notification & Actions
然后点击开关打开通知(问题解决)