Skip to content

Instantly share code, notes, and snippets.

@paulgibbs
Created July 29, 2012 14:46
Show Gist options
  • Save paulgibbs/3199341 to your computer and use it in GitHub Desktop.
Save paulgibbs/3199341 to your computer and use it in GitHub Desktop.
Web Notifications POC
// very rough, basic javascript calls to use the W3 Web Notifications api, rather than all the webkitNotifications stuff which you find in google.
(function($) {
$(document).ready(function() {
// Ask for permission
$('#ask_permission').click(function(e) {
e.preventDefault();
// webkitNotifications.requestPermission(function(){}); // use this for safari / w3 standards
// window.Notification.requestPermission(function(e) { alert(e); }); // use this for chrome (w3 standard causes a crash)
});
// Display the plain text notification
// 'tag' replaces the existing notification, to prevent duplicate achievements
$('#plain_form').submit(function(e) {
new Notification("New Email mo", { tag: 'again', body: "moo world" } );
});
});
})(jQuery);
@jonathan-fielding
Copy link

Interesting, what are you trying to achieve with this POC, I saw you have watched my notifications plugin for jquery, your welcome to help out or post suggestions on the issues board

@paulgibbs
Copy link
Author

I'm looking for a lightweight Web Notifications implementation that uses the window.Notification format (per the spec), rather than the webkitNotifications support. While searching to see if there was a polyfil for Firefox/IE9 I found your new github project, so am interested to see what it grows into.

The above gist is basically just a note to myself as I'd spent a while trying to get it working on a test page.

@jonathan-fielding
Copy link

ah ok, im using $.notification instead mainly because Safari's is half there with window.Notification but is mostly using the webkitNotifications. Do a clone of what I have done so far and try it out in different browsers, working quite well at moment, one limitation I have found in chrome is that you seem to have to have the user click something before you can request permission (tweeting Paul Irish confirmed this)

@paulgibbs
Copy link
Author

What more of the spec is there beyond requesting permission , and adding a notification?

Thanks for the heads-up about Chrome; that's annoying. I'll have to test triggering a click event on some hidden object, on page load.

@jonathan-fielding
Copy link

tried that in chrome, still same error so in my plugin you have 2 options,

$.notifications.requestPermission() - basically you have to add this to an element's click event for it to work in chrome
$.notifications.notificationPrompt() - this shows a overlay explaining the site needs to use notifications, and then when user clicks to close overlay this is is able to fire $.notifications.requestPermission();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment