javascript - Electron 通知 API 'click' 事件不起作用

标签 javascript node.js electron

我有一个问题,experimental electron notification 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() 的调用是未定义(不返回任何内容)。

通过将代码行分成两个语句,可以很容易地解决这个问题:

notification = new Notification({title: "Message Received",body: "message body"})

notification.show()

notification.on('click', (event, arg)=>{
  console.log("clicked")
})

关于javascript - Electron 通知 API 'click' 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61189074/

相关文章:

javascript - 如何像 C# 一样在 Javascript 中传递手动参数顺序?

node.js - IORedis 或 (node_redis) 回调在调用自定义 Redis 命令/模块后未触发

json - 如何使用 node.js 访问 json 对象的子元素

angular - 将 Angular 13 用于 Electron 应用程序时如何升级 webpack 5?

javascript - 使用 React-Native-Router-Flux 从导航栏按钮传递 Prop

javascript - 使用 jQuery 检查父复选框

javascript - 使用 javascript 运行客户端 powershell 脚本

node.js - 在NodeJS端使用SocketIo读取数据?

javascript - Electron 渲染器过程: When Should I Clean IPC Listeners

angular - Webpack外部组件不需要使用Electron和Angular 4定义