@indutny/simple-windows-notifications
Version:
Simple Windows Notifications
86 lines (85 loc) • 2.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Notifier = void 0;
exports.sendDummyKeystroke = sendDummyKeystroke;
const bindings_1 = __importDefault(require("bindings"));
let binding;
if (process.platform === 'win32') {
binding = (0, bindings_1.default)({
bindings: 'simple-windows-notifications',
try: [
[
'module_root',
'prebuilds',
`${process.platform}-${process.arch}`,
'@indutny+simple-windows-notifications.node',
],
['module_root', 'build', 'Release', 'bindings'],
['module_root', 'build', 'Debug', 'bindings'],
],
});
}
/**
* Main class
*/
class Notifier {
appId;
/**
* @constructor
* @param appId - Application id, typically: 'org.nodejs.node'
*/
constructor(appId) {
this.appId = appId;
}
/**
* Show a notification with a given toast XML.
*
* @param toastXml - See https://learn.microsoft.com/en-us/previous-versions/windows/apps/hh761494(v=win.10)
* @param options - Notification data use to identify the notification.
*/
show(toastXml, { tag, group, expiresOnReboot = false }) {
if (!binding) {
throw new Error('This library works only on Windows');
}
binding.showNotification(this.appId, toastXml, tag, group, expiresOnReboot);
}
/**
* Remove a notification with a given tag/group
*
* @param toastXml - See https://learn.microsoft.com/en-us/previous-versions/windows/apps/hh761494(v=win.10)
* @param options - Notification data use to identify the notification.
*/
remove({ tag, group }) {
if (!binding) {
throw new Error('This library works only on Windows');
}
binding.removeNotification(this.appId, tag, group);
}
/**
* Remove all notifications sent by this app.
*/
clearAll() {
if (!binding) {
throw new Error('This library works only on Windows');
}
binding.clearHistory(this.appId);
}
}
exports.Notifier = Notifier;
/**
* Send a dummy keystroke to the app. Needed when starting a second instance
* of the app from the notification manager and bringing the first instance to
* the foreground.
*
* See: https://chromium.googlesource.com/chromium/src.git/+/5c432815bbb22210f7c995bbb508359f64baadf5/chrome/notification_helper/notification_activator.cc#155
* See: https://www.npmjs.com/package/windows-dummy-keystroke#but-why
*/
function sendDummyKeystroke() {
if (!binding) {
throw new Error('This library works only on Windows');
}
binding.sendDummyKeystroke();
}