seriema / electron-notification-shim

Get Notification API events in Electron main-process. Perfect for adding Notification toasters in Windows with node-notifier or other solution.
MIT License
37 stars 5 forks source link

Override and display our own notification #4

Open sujithv28 opened 8 years ago

sujithv28 commented 8 years ago

Can we do something like event.preventDefault() and then create our own notification to display with the msg parameter?

The main reason I want to do this is because I want to detect when the user taps on the notification so I can redirect to a different URL and unhide the app.

seriema commented 8 years ago

Something like PR #3 ?

sujithv28 commented 8 years ago

Exactly, what would the code for that look like?

seriema commented 8 years ago

Not sure, since I asked for documentation samples but haven't received any. I'll try to look at creating some myself soon.

sujithv28 commented 8 years ago

@seriema did you or anyone have any success implementing this in code?

seriema commented 8 years ago

Honestly, I haven't tried yet. I'll try to do it soon.

sujithv28 commented 8 years ago

@seriema any luck?

akashnimare commented 8 years ago

@sujithv28 Did you mean override or hide the native notifications and then show them with your own styling?

sujithv28 commented 8 years ago

@akashnimare I mean override the notifications so I could add an on-click listener which currently isn't supported.

sujithv28 commented 8 years ago

@akashnimare @seriema Any updates on when this feature will be pushed?

akashnimare commented 8 years ago

Not yet.

seriema commented 8 years ago

@sujithv28 you mean a click handler on the native event? Or on the Electron side? I think I could use some fake sample code of what you imagine you'd like to do, or at least some pretend screenshots/images.

sujithv28 commented 8 years ago

@seriema So lets say the website you loaded in electron receives an HTML5 notification. As intended, the notification-shim will intercept that and send out a native notification on the Mac with the content of the HTML5 notification sent from the web. The problem is, since that notification object is created by the notification-shim and we have no way of accessing it, I cant add a "onclick" listener to it to know when it was clicked on, or have an option to override it.

seriema commented 8 years ago

@sujithv28 Sorry for the long delay on this. I've experimented with this and the PR #3. The problem is that adding a click handler to Notification is usually done by just assigning a method to the notification object, like this: notification.onclick = function () .... Since electron-notification-shim overrides the constructor any onclick handler it adds will be replaced immediately after. I did a sample branch for you to try if you'd like. Maybe you have some ideas? https://github.com/seriema/electron-notification-shim/tree/notification-onclick-issue (Check the last commit. Currently 5e3abfe)

Using a setTimeout would only solve some cases. If you control your web app then you can opt to attach an event handler instead of overriding onclick, but as a library I need to consider more cases. Thoughts?

The same goes for sending an ipc event. We need to override onclick to do that.

sujithv28 commented 8 years ago

@seriema What about supporting something like event.preventDefault() so we can choose when and when not to actually present the notification. By muting notification-shim from creating its own notification object and presenting it, I can create my own locally using the msg contents and add a listener on my side as well.

seriema commented 8 years ago

@sujithv28 The notification goes out the moment it's created (new Notification()). The second parameter is the options (MDN) and it has things like silent. What I could do is that you can pass a setting to electron-notification-shim that will basically remove all native notifications so that you can use your own. Not sure yet how it would be nicely implemented. It does go beyond being a shim so I'm wondering if that wouldn't be better done in another library. My goal has always been for a "plug and play" approach for apps that have notifications working in Electron but want to support Windows.