Advertisement
shifattk

Push Notification Code JS

Oct 28th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Desktop notifications are only available on WebKit browsers, for now, so only carry out
  2. // notifications if the API is available.
  3. if (window.webkitNotifications) {
  4.  
  5.     // Just a piece of data to determine whether 1) the page just loaded, and 2) there are any
  6.     // new elements in the feed.
  7.     var lastTime = null;
  8.  
  9.     // This is to check if you have permissions to display notifications. Usually, the
  10.     // checkPermissions method only returns a number. 0 being that you don't have permission
  11.     // to display notifications, yet.
  12.     if (window.webkitNotifications.checkPermissions() <= 0) {
  13.         // If you don't have permission, then get the permission from the user.
  14.         window.webkitNotifications.requestPermission(callback);
  15.     }
  16.  
  17.     // The code below will run every thirty seconds.
  18.     setInterval(function () {
  19.         // What we want to do here is carry out an AJAX request to check for changes in the RSS
  20.         // feed only if we have permission to display notifications. Otherwise, what's the point
  21.         // of checking for changes if the user doesn't want to know?
  22.         if (window.webkitNotifications.checkPermissions() > 0) {
  23.             $.jFeed({
  24.                 url: 'http://masseyappnews.wordpress.com/feed',
  25.                 success: function (feed) {
  26.                     // Looks at the latest item's time, and checks to see if it's any different
  27.                     // than what we have in memory.
  28.                     if (lastTime !== feed.items[0].updated) {
  29.  
  30.                         // If so, determine whether we recorded the time, or not.
  31.                         if (lastTime !== null) {
  32.                             // If we did record the time, that means there are new content from
  33.                             // the last time we checked.
  34.                             window.webkitNotifications()
  35.                         }
  36.  
  37.                         // Update the last time.
  38.                         lastTime = feed.items[0].updated;
  39.                     }
  40.                 }
  41.             });
  42.         }
  43.     }, 30000);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement