javascriptnode.jselectron

Electron notification API 'click' event not working


I have a problem where the experimental electron notification API doesn't submit the 'click' event or I am simply using it wrong however I want to be clear this is the new notification system that runs in the main process not in the renderer process:

My Code:

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

Things tried:

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

Solution

  • There is a small flaw in your code: right now, the variable notification does not receive the result of the call to new Notification(), but instead the result of the call to show() which is undefined (returns nothing).

    This is fairly easy to fix by splitting the line of code into two statements:

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