Ad

How Do I Simultaneously Fire A JavaScript Alert And A Notification In Electron?

- 1 answer

I am using web app build-ed with Electron.

Code below uses Electron notification let myNotification for OSX system notifications. And JS alert sends regular alert to application window.

   var timerIDtwo = setInterval(function() {
    if (navigator.onLine) {
         var date = new Date();
         var time = date.toLocaleTimeString();
         let myNotification = new Notification('TRENUTNO STE ONLINE!', {
                                      body: 'Baza podataka je dostupna!\n\nVrijeme poruke:'+time
                                    });
        alert('TRENUTNO STE ONLINE!\n\nBaza podataka je dostupna!\n\nVrijeme poruke: '+time); 
        //     clearInterval(timerID);
} else {
//else
}
}, 10 * 1000);

Problem:

OS notification is fired ONLY after I click OK on alert message. It should fire both in same time not dependent of each other. As soon as I remove one other works properly.

I tried separating one in new function within each other and all sorts of thing like changing order and so on but it always fire after the alert is closed.

Please advise.

EDIT: If i separate them and fire notification first it fires as wanted notification then 2 sec later alert, but next notification is fired again only after alert is closed.

<script type="text/javascript">
   var timerID = setInterval(function() {
    if (navigator.onLine) {
         var date = new Date();
         var time = date.toLocaleTimeString();
         let myNotification = new Notification('TRENUTNO STE ONLINE!', {
                                      body: 'Baza podataka je dostupna!\n\nVrijeme poruke:'+time
                                    });
} else {
//
}
}, 10 * 1000); 
</script>


<script type="text/javascript">
   var timerID = setInterval(function() {
    if (navigator.onLine) {
         var date = new Date();
         var time = date.toLocaleTimeString();
        alert('TRENUTNO STE ONLINE!\n\nBaza podataka je dostupna!\n\nVrijeme poruke: '+time); 
        //     clearInterval(timerID);
} else {
//
}
}, 12 * 1000); 
</script>
Ad

Answer

As the other answer stated, execution in the renderer process is paused when you call alert().

In order to get around this limitation, you can use Electron's Notification API to spawn the notification from the main process instead.

Use IPC to communicate from your renderer to the main process that you wish to trigger a notification.

// main.js
const { Notification, ipcMain } = require('electron')

ipcMain.on('notification', () => {
  const n = new Notification({
    title: "Notification",
    body: "Not blocked by alert!"
  });
  n.show()
})
// renderer.js
const { ipcRenderer } = require('electron')

ipcRenderer.send('notification')
alert('I am an alert.')

Even though the renderer process will still be blocked after the execution of the alert, the main process is unaffected and will be able to trigger the notification.

See this runnable example openable in Electron Fiddle.

Ad
source: stackoverflow.com
Ad