shared/scripts/lib/notifications.js
/**
 * Listens for Chrome alarms to trigger the next notification.
 */
Listens for Chrome alarms to trigger the next notification.
Listens for Chrome alarms to trigger the next notification .
function listen(worker) {
  if (environment === 'chrome') {
    storage.get('settings').then(function(settings) {
      var createNotification = function(name) {
        if (settings.moneyIsOwed) {
          console.log('creating');
          chrome.notifications.create("tipsy", {
            type: 'basic',
            iconUrl: '../img/logo64.png',
            title: 'Tipsy',
            message: 'Time to Donate!'
          }, function unhandledCallback() {
            var days = toDays[settings.reminderLevel];
            var next = new Date(settings.nextNotified);
            next.setDate(next.getDate() + days);
            settings.nextNotified = Number(next);
            create(name, next, days);
            storage.set('settings', settings);
          });
          addClickable();
        }
      };
      if (Number(settings.nextNotified) < Date.now()) {
        createNotification();
      }
      chrome.alarms.onAlarm.addListener(function(alarm) {
        createNotification(alarm.name);
      });
    });
    pollForNotifications();
    setInterval(pollForNotifications, 3600000);
  }
  else if (environment === 'firefox') {
    var notifications = require('sdk/notifications');
    var timers = require('timers');
    var timeout;
    var nextNotified = storage.engine.nextNotified;
    worker.port.on('notification.set', function(days) {
      timers.clearTimeout(timeout);
      nextNotified = storage.engine.nextNotified;
    });
  }
}
function listen ( worker ) { if ( environment === 'chrome' ) { storage . get ( 'settings' ) . then ( function ( settings ) { var createNotification = function ( name ) { if ( settings . moneyIsOwed ) { console . log ( 'creating' ) ; chrome . notifications . create ( "tipsy" , { type : 'basic' , iconUrl : '../img/logo64.png' , title : 'Tipsy' , message : 'Time to Donate!' } , function unhandledCallback ( ) { var days = toDays [ settings . reminderLevel ] ; var next = new Date ( settings . nextNotified ) ; next . setDate ( next . getDate ( ) + days ) ; settings . nextNotified = Number ( next ) ; create ( name , next , days ) ; storage . set ( 'settings' , settings ) ; } ) ; addClickable ( ) ; } } ; if ( Number ( settings . nextNotified ) < Date . now ( ) ) { createNotification ( ) ; } chrome . alarms . onAlarm . addListener ( function ( alarm ) { createNotification ( alarm . name ) ; } ) ; } ) ; pollForNotifications ( ) ; setInterval ( pollForNotifications , 3600000 ) ; } else if ( environment === 'firefox' ) { var notifications = require ( 'sdk/notifications' ) ; var timers = require ( 'timers' ) ; var timeout ; var nextNotified = storage . engine . nextNotified ; worker . port . on ( 'notification.set' , function ( days ) { timers . clearTimeout ( timeout ) ; nextNotified = storage . engine . nextNotified ; } ) ; } }
Listens for Chrome alarms to trigger the next notification.
Listens for Chrome alarms to trigger the next notification .
// Once the alarm triggers, create a notification to dispaly to the // user. // Reset the next notified in the storage engine. // Create the next alarm. // Check for notifications and then poll every hour. // Create a notification timeout upon launching that figures out when, the // next reminder should trigger. // Cache this value for easier access. // Reset the current notificaiton. // Cache this value for easier access. //timeout = setTimeout(showNotification, Date.now() - nextNotified); // Reusable function to show the extension and reset. //var showNotification = function() { //  // Always reset the timeout. //  timers.clearTimeout(timeout); //  notifications.notify({ //    title: 'Tipsy', //    text: 'Time to donate!' //  }); //  // Set the next notification. //  var days = reminderLevelToDays[storage.engine.reminderLevel]; //  storage.engine.nextNotified = Date.now() * (days * 1440 * 60000); //  // Convert the new date to milliseconds. //  var milliseconds = storage.engine.nextNotified; //  // Set the next timeout. //  timeout = timers.setTimeout(showNotification, Date.now() - milliseconds); //}; //// If this notification is scheduled for the future, set a timeout. //if (nextNotified > 0) { //  // Set a timeout with the difference until the notification should //  // trigger. //  timeout = timers.setTimeout(showNotification, Date.now() - nextNotified); //} //// Otherwise immediately show and schedule for the next one. //else { //  showNotification(); //}