Skip to content

Commit

Permalink
feat: -persistent flag for win10/11 notifications keeps them on-scree…
Browse files Browse the repository at this point in the history
…n until dismissed
  • Loading branch information
Aetherinox committed Jul 10, 2024
1 parent 55327ba commit 0d02978
Showing 1 changed file with 50 additions and 14 deletions.
64 changes: 50 additions & 14 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const inArray = function (arr, val) {
return arr.indexOf(val) !== -1;
};

/*
notifySend Flags
*/

const notifySendFlags = {
u: 'urgency',
urgency: 'urgency',
Expand Down Expand Up @@ -81,6 +85,7 @@ module.exports.fileCommandJson = function (notifier, options, cb) {
console.info('[notifier path]', notifier);
console.info('[notifier options]', options.join(' '));
}

return cp.execFile(notifier, options, function (error, stdout, stderr) {
if (error) return cb(error, stdout);
if (!stdout) return cb(error, {});
Expand Down Expand Up @@ -161,16 +166,19 @@ module.exports.mapToNotifySend = function (options) {
if (options.timeout === false) {
delete options.timeout;
}

if (options.wait === true) {
options['expire-time'] = 5; // 5 seconds default time (multipled below)
}

for (const key in options) {
if (key === 'message' || key === 'title') continue;
if (options.hasOwnProperty(key) && notifySendFlags[key] !== key) {
options[notifySendFlags[key]] = options[key];
delete options[key];
}
}

if (typeof options['expire-time'] === 'undefined') {
options['expire-time'] = 10 * 1000; // 10 sec timeout by default
} else if (typeof options['expire-time'] === 'number') {
Expand Down Expand Up @@ -215,10 +223,19 @@ module.exports.mapToMac = function (options) {
options.sound = 'Bottle';
}

/*
wait : Wait with callback, until user action is taken against notification,
does not apply to Windows Toasters as they always wait or notify-send
as it does not support the wait option
timeout : Takes precedence over wait if both are defined.
*/

if (options.wait === true) {
if (!options.timeout) {
options.timeout = 5;
}

delete options.wait;
}

Expand Down Expand Up @@ -325,20 +342,31 @@ function removeNewLines(str) {
}

/*
---- Options ----
[-t] <title string> | Displayed on the first line of the toast.
[-m] <message string> | Displayed on the remaining lines, wrapped.
[-b] <button1;button2 string>| Displayed on the bottom line, can list multiple buttons separated by ";"
[-tb] | Displayed a textbox on the bottom line, only if buttons are not presented.
[-p] <image URI> | Display toast with an image, local files only.
[-id] <id> | sets the id for a notification to be able to close it later.
[-s] <sound URI> | Sets the sound of the notifications, for possible values see http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx.
[-silent] | Don't play a sound file when showing the notifications.
[-appID] <App.ID> | Don't create a shortcut but use the provided app id.
[-pid] <pid> | Query the appid for the process <pid>, use -appID as fallback. (Only relevant for applications that might be packaged for the store)
[-pipeName] <\.\pipe\pipeName\> | Provide a name pipe which is used for callbacks.
[-application] <C:\foo.exe> | Provide a application that might be started if the pipe does not exist.
-close <id> | Closes a currently displayed notification.
Ntfy Toast / Toaster
Windows 10 & 11 use ntfy-toast library to send notifications:
https://github.com/Aetherinox/ntfy-toast
ntfy-toast has a special parameter for ensuring that notifications stick and do dismiss
unless the user physically dismisses them by using:
-persistent
---- Options ----
[-t] <title string> | Displayed on the first line of the toast.
[-m] <message string> | Displayed on the remaining lines, wrapped.
[-b] <button1;button2 string> | Displayed on the bottom line, can list multiple buttons separated by ";"
[-tb] | Displayed a textbox on the bottom line, only if buttons are not presented.
[-p] <image URI> | Display toast with an image, local files only.
[-id] <id> | sets the id for a notification to be able to close it later.
[-s] <sound URI> | Sets the sound of the notifications, for possible values see http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx.
[-silent] | Don't play a sound file when showing the notifications.
[-persistent] | Makes the notification stick until the user dismisses it.
[-d] (short | long) | Set the duration default is "short" 7s, "long" is 25s.
[-appID] <App.ID> | Don't create a shortcut but use the provided app id.
[-pid] <pid> | Query the appid for the process <pid>, use -appID as fallback. (Only relevant for applications that might be packaged for the store)
[-pipeName] <\.\pipe\pipeName\> | Provide a name pipe which is used for callbacks.
[-application] <C:\foo.exe> | Provide a application that might be started if the pipe does not exist.
-close <id> | Closes a currently displayed notification.
*/
const allowedToasterFlags = [
't',
Expand All @@ -348,13 +376,16 @@ const allowedToasterFlags = [
'p',
'id',
's',
'd',
'silent',
'persistent',
'appID',
'pid',
'pipeName',
'close',
'install'
];

const toasterSoundPrefix = 'Notification.';
const toasterDefaultSound = 'Notification.Default';
module.exports.mapToWin8 = function (options) {
Expand Down Expand Up @@ -402,6 +433,11 @@ module.exports.mapToWin8 = function (options) {
delete options.sound;
}

if (options.wait === true) {
options.persistent = true;
delete options.wait;
}

if (options.s === false) {
options.silent = true;
delete options.s;
Expand Down

0 comments on commit 0d02978

Please sign in to comment.