stackexchange-notifications icon indicating copy to clipboard operation
stackexchange-notifications copied to clipboard

Add option to use native notifications instead of Chrome's notifications

Open Rob--W opened this issue 9 years ago • 4 comments

A user wanted native notifications in Chrome, so I offered the following advice:

Open chrome://extensions, enable developer mode. Click on Inspect background page at my extension. Click on the sources tab, select "using-websocket.js" and set a breakpoint at the first line. Then run delete chrome.notifications; in the console. Then copy-paste https://gist.github.com/Rob--W/5926727 in the console and run it. Then continue the breakpoint, and generate a notification (e.g. by calling setUnreadCount(1); ). Does that give the desired result?

This seems to give the desired result, so let's properly implement this:

  • [ ] Add new checkbox option to options.html
  • [ ] Bind an event listener to the checkbox in options.js that toggles a value in localStorage.
  • [ ] Modify using-websockets.js to detect whether this preference is set, and if so, use the Notification constructor instead of chrome.notifications to display the notification.

Rob--W avatar Apr 19 '16 15:04 Rob--W

Hullo,

I did notice a bugz with the above.

The notifications do pop up, but when clicking on them, stackoverflow doesn't open. I.e, nothing happens when you click on the notification.

Normally when clicking on a notification (ex Facebook msg): if FB tab exists, -- it activates that tab and loads message else -- creates new tab and loads page with message remove notification from notification tray.

Any ideas on how to add click hander?

LeoUfimtsev avatar Apr 19 '16 15:04 LeoUfimtsev

Have you tried assigning a click handler to the onclick property? https://github.com/Rob--W/stackexchange-notifications/blob/c16a3df5c9b25139b7e25acb1fdfbf857319f6e0/Chrome/using-websocket.js#L241 should be changed to this:

        var useNativeNotifications = localStorage.getItem('name of the preference') === '1';
        if (window.Notification && useNativeNotifications) {
            // See https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification
            _notification = new Notification(...);
            _notification.onclick = function() {
                openTab(getLink() || generateDefaultLink());
            };
            return;
        }
        if (!window.webkitNotifications) {
            ....

Also https://github.com/Rob--W/stackexchange-notifications/blob/c16a3df5c9b25139b7e25acb1fdfbf857319f6e0/Chrome/using-websocket.js#L243 should be changed to also account for both ways of closing the notification (the old webkitNotifications API used cancel, the new one uses close()).

Rob--W avatar Apr 19 '16 15:04 Rob--W

Thank you for suggestion. I have my final university exam on Thursday. Then I'm done (with Uni ^_^). I'll try the above Friday/weekend and will get back to you.

LeoUfimtsev avatar Apr 19 '16 15:04 LeoUfimtsev

@LeoUfimtsev Best of luck with your exams!

Rob--W avatar Apr 19 '16 21:04 Rob--W