node-notifier icon indicating copy to clipboard operation
node-notifier copied to clipboard

Non-blocking notifications

Open hecerinc opened this issue 5 years ago • 1 comments

Is there any way that the windows toast notifications does not block the command line? i.e. Can you enable the wait: false or timeout: false for Windows notifications?

hecerinc avatar Jan 22 '20 08:01 hecerinc

Non-blocking notifications would be great, i hope someone will take a look at this option. My current workaround is spawning a detached process of the notifier, sending options through arguments and kill the process with a timer:

CAVEATS:

  1. You cannot read the detached process output since it's no longer bound to the main process.
  2. If you set the timer too low, the process will be killed before the notifier gets to initialize.

detached.notifier.js

// 300ms seems to do the work. 
// You could increase it to 400-500ms if you feel that 
// the notifier needs more time to initialize

const notifier = require('node-notifier');

 // Base64 > JSON(Parse) > Options 
notifier.notify(JSON.parse(Buffer.from(process.argv[2], 'base64').toString()));
setTimeout(() => {
    process.exit();
}, 300); 

And the method:

const detachedNotifier = (options = {}) => {

        const {spawn} = require('child_process');
        const processArguments = [
                  // Path to detached.notifier.js
                 __dirname + '/detached.notifier.js', 
                 
                 // Options > JSON(Stringify) > Base64
                 // And make sure the whole string is in double quotes
                 // so it can be grouped as a whole argument.
                 `"${Buffer.from(JSON.stringify(options)).toString('base64')}"` 
        ];

        spawn('node', processArguments, {
            detached: true, // this must be true
            windowsHide: true
        })

    }

darklightcode avatar Feb 21 '21 18:02 darklightcode