I have a problem with the Electron notification API . I always got the notification but very often the click event on notification is not executed. Sometimes click event will be executed only the first 2 times, sometimes only the tenth time, and sometimes not at all.
The video file with problem: VIDEO
There is my code:
function showNotification () {
const notificationOptions = {
title: 'SysInfoGrabber',
body: 'Raport PDF został utworzony na pulpicie w folderze "Raporty"',
icon: path.join(__dirname, 'files/icon.png')
}
const reportNotification = new Notification(notificationOptions);
reportNotification.on('show', () => {
console.log('Notification is shown');
});
reportNotification.on('click', () => {
console.log("Notification clicked");
});
reportNotification.show();
}
mainFunction(){
[...CODE...]
// Show notify
showNotification();
}
#npm i node-notifier#
const notifier = require('node-notifier');
notifier.notify({
appID: 'myApp',
title: ' ',
message: 'myMesaage',
wait: true
}, function (err, response) {
if (response !== 'timeout'){
console.log("Notification clicked");
}
});
The issue you're running into is probably caused by the fact that the constant reportNotification
is declared locally inside the showNotification()
function, and will get garbage-collected some time after the function is exited, and the event handlers attached to it won't be usable any longer.
Try to declare reportNotification
globally as a variable (using let
) outside any other function(s).
let reportNotification;
function showNotification () {
const notificationOptions = {
title: 'SysInfoGrabber',
body: 'Raport PDF został utworzony na pulpicie w folderze "Raporty"',
icon: path.join(__dirname, 'files/icon.png')
}
reportNotification = new Notification(notificationOptions);
reportNotification.on('show', () => {
console.log('Notification is shown');
});
reportNotification.on('click', () => {
console.log("Notification clicked");
});
reportNotification.show();
}
mainFunction(){
[...CODE...]
// Show notify
showNotification();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.