我正在尝试制作一个简单的应用程序,该应用程序应在单击按钮时显示通知。问题是通知没有显示,但console.logs 显示。通知是否应该适用于开发模式?(意味着只是运行electron .
,我不必构建和安装应用程序)
视窗操作系统:
- 版本:Windows 10 家庭版
- 版本:1709
- 构建:16299.98
- 注意:Toast 在下方启用(横幅、操作中心)
System->Notification & Actions
代码:
// 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>