电子通知 API“点击”事件不起作用

fut*_*502 0 javascript node.js 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
Run Code Online (Sandbox Code Playgroud)

尝试的事情:

notification.once('click', (event, arg)=>{
  console.log("clicked")
})
Run Code Online (Sandbox Code Playgroud)
notification.onclick = () =>{
  console.log("clicked")
}
Run Code Online (Sandbox Code Playgroud)

Gue*_*Who 6

有一个在你的代码中的小瑕疵:现在,变量notification不接收调用的结果new Notification(),而是调用的结果show()undefined(返回什么)。

这很容易通过将代码行拆分为两个语句来解决:

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

notification.show()

notification.on('click', (event, arg)=>{
  console.log("clicked")
})
Run Code Online (Sandbox Code Playgroud)