Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for notification overrides #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const ipc = require('ipc');
module.exports = () => {
const OldNotification = Notification;

var settings = {
forceSilent: false,
bodyOverride: undefined
};

Notification = function (title, options) {
// Send this to main thread.
// Catch it in your main 'app' instance with `ipc.on`.
Expand All @@ -16,12 +21,29 @@ module.exports = () => {
options
});

// Shim onclick event
var notification;
setTimeout(function () {
var onclickOld = notification.onclick;
notification.onclick = function () {
ipc.send('notification-onclick-shim', notification);
if (onclickOld) onclickOld();
};
}, 1);

// Apply overrides
options = Object.assign({}, options);
if (settings.forceSilent) options.silent = true;
if (settings.bodyOverride) options.body = settings.bodyOverride;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's actually happening here. From the comment it seems that options should override settings, but the original values of settings is what seems to be used? Is the returned settings meant to be changed by whoever uses the library, and can be changed at runtime?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the bodyOverride: is it to override the notifications when notifications are handled natively? If so, maybe it could be nice to override the icon as well? Hmm

I like the forceSilent. Would there be a benefit to killing the notification all together when that happens, as compared to setting the silent flag?


// Send the native Notification.
// You can't catch it, that's why we're doing all of this. :)
return new OldNotification(title, options);
return notification = new OldNotification(title, options);
};

Notification.prototype = OldNotification.prototype;
Notification.permission = OldNotification.permission;
Notification.requestPermission = OldNotification.requestPermission;

return settings;
};