![](/img/trans.png)
[英]Electron (main) notification API 'on.click' event not always working
[英]Electron notification API 'click' event not working
我有一个问题,实验性的 electron 通知 API没有提交“点击”事件,或者我只是用错了它,但是我想清楚这是在主进程中运行的新通知系统,而不是在渲染器进程中运行:
我的代码:
notification = new Notification({title: "Message Received",body: "message body"}).show()
// The above works and a notification gets made
notification.on('click', (event, arg)=>{
console.log("clicked")
})
// The above gives an error about 'on' not being defined
尝试过的事情:
notification.once('click', (event, arg)=>{
console.log("clicked")
})
notification.onclick = () =>{
console.log("clicked")
}
您的代码中有一个小缺陷:现在,变量notification
没有收到调用new Notification()
的结果,而是收到调用show()
的结果,它是undefined
的(不返回任何内容)。
通过将代码行拆分为两个语句,这很容易解决:
notification = new Notification({title: "Message Received",body: "message body"})
notification.show()
notification.on('click', (event, arg)=>{
console.log("clicked")
})
您需要在调用 show 方法之前处理点击事件。 下面的代码在 Electron 10.1.1 上对我有效(不确定在停止工作之前你能回到 go 多远)
notification = new Notification({ title: "Test Title", body: "Test Body" });
notification.on('click', (event, arg) => {
console.log("clicked");
});
notification.show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.