【问题标题】:Electron notification API 'click' event not working电子通知 API“点击”事件不起作用
【发布时间】:2020-07-26 01:58:05
【问题描述】:

我有一个问题,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")
}

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:

    您的代码中有一个小缺陷:现在,变量notification 没有收到对new Notification() 的调用结果,而是对show() 的调用结果undefined(什么都不返回)。

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

    notification = new Notification({title: "Message Received",body: "message body"})
    
    notification.show()
    
    notification.on('click', (event, arg)=>{
      console.log("clicked")
    })
    

    【讨论】:

    • 我这样做了,没有出现错误,但它也没有注册点击。那就是......它从不控制台记录任何东西。 :\
    • 我遇到了同样的问题(Electron 11.1.1)
    【解决方案2】:

    您需要在调用 show 方法之前处理 click 事件。下面的代码在我的 Electron 10.1.1 上运行(不确定在停止工作之前你能走多远)

    notification = new Notification({ title: "Test Title", body: "Test Body" });
    
    notification.on('click', (event, arg) => {
      console.log("clicked");
    });
    
    notification.show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多