Advertisement
bali182

Untitled

Mar 19th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // The NotificationHandler "class"
  2. var NotificationHandler =
  3. {
  4.     DATABASE_NAME: "notifications",
  5.     SINGLETIME_TABLE : "singletime",
  6.     REPEATED_TABLE : "repeated",
  7.     SERVICE_PROPERTY: "service_running",
  8.    
  9.     mergeObjects:function(defaultObj, newObj)
  10.     {
  11.         if(typeof defaultObj != 'object')
  12.             defaultObj = {};
  13.         if(typeof newObj != 'object')
  14.             newObj = {};
  15.         for(var property in newObj)
  16.             if(newObj[property] != null && newObj[property] != undefined)
  17.             defaultObj[property] = newObj[property];
  18.         return defaultObj;
  19.     },
  20.    
  21.     clear : function()
  22.     {
  23.         var database = Titanium.Database.open(this.DATABASE_NAME);
  24.         database.execute("DROP TABLE IF EXISTS "+this.SINGLETIME_TABLE);
  25.         database.execute("DROP TABLE IF EXISTS "+this.REPEATED_TABLE);
  26.         database.close();
  27.         this.initialize();
  28.     },
  29.    
  30.     initialize : function()
  31.     {
  32.         var database = Titanium.Database.open(this.DATABASE_NAME);
  33.         database.execute("CREATE TABLE IF NOT EXISTS " + this.SINGLETIME_TABLE + " (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(255), message VARCHAR(255), year INTEGER, month INTEGER, day INTEGER, hours INTEGER, minutes INTEGER)");
  34.         database.execute("CREATE TABLE IF NOT EXISTS " + this.REPEATED_TABLE + " (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(255), message VARCHAR(255), day INTEGER, hours INTEGER, minutes INTEGER)");
  35.         database.close();
  36.        
  37.         var isRunning = Ti.App.Properties.getBool(this.SERVICE_PROPERTY, false);   
  38.         Ti.API.info("running: "+isRunning);
  39.        
  40.         var intent = Titanium.Android.createServiceIntent({url : 'notification/NotificationService.js' });
  41.         intent.putExtra('interval', 1000);
  42.        
  43.         Titanium.Android.stopService(intent);
  44.        
  45.         var service = Titanium.Android.createService(intent);
  46.                
  47.         service.addEventListener('start', function()
  48.         {
  49.             Ti.API.info("Started");
  50.             Ti.App.Properties.setBool(NotificationHandler.SERVICE_PROPERTY, true);
  51.         });
  52.        
  53.         service.addEventListener('stop', function()
  54.         {
  55.             Ti.API.info("Stoped");
  56.             Ti.App.Properties.setBool(NotificationHandler.SERVICE_PROPERTY, false);
  57.         });
  58.        
  59.         service.start();       
  60.     },
  61.    
  62.     addNotification: function(data)
  63.     {
  64.         var defaults =
  65.         {
  66.             title: 'Notification',
  67.             message: 'Message',
  68.             singleTime: new Date().getTime()+(1*60*1000),
  69.             repeatDay: -1,
  70.             repeatHour: 12,
  71.             repeatMinute: 0,
  72.             repeatWeekly: false,
  73.         };
  74.        
  75.         data = this.mergeObjects(defaults, data);
  76.        
  77.         var database = Titanium.Database.open(this.DATABASE_NAME);
  78.        
  79.         if(data.repeatWeekly)
  80.         {
  81.             database.execute("INSERT INTO "+this.REPEATED_TABLE+" VALUES(?,?,?,?,?,?)", null, data.title, data.message, data.repeatDay, data.repeatHour, data.repeatMinute);
  82.         }
  83.         else
  84.         {
  85.             var date = new Date(data.singleTime)
  86.             var year = date.getFullYear();
  87.             var month = date.getMonth();
  88.             var day = date.getDate();
  89.             var hours = date.getHours();
  90.             var mins = date.getMinutes();
  91.    
  92.             database.execute("INSERT INTO "+this.SINGLETIME_TABLE+" VALUES(?,?,?,?,?,?,?,?)", null, data.title, data.message, year, month, day, hours, mins);
  93.         }
  94.     },
  95.    
  96.     getSingletimeNotifications : function(year, month, day, hours, mins)
  97.     {
  98.         var database = Titanium.Database.open(this.DATABASE_NAME);
  99.         var resultSet = database.execute("SELECT id, title, message FROM "+this.SINGLETIME_TABLE+" WHERE year=? AND month=? AND day=? AND hours=? AND minutes=?", year, month, day, hours, mins);
  100.         var result = [];
  101.    
  102.         while(resultSet.isValidRow())
  103.         {
  104.             var id = parseInt(resultSet.fieldByName("id"));
  105.             var title = resultSet.fieldByName("title");
  106.             var message = resultSet.fieldByName("message");
  107.             var type = "single";
  108.            
  109.             result.push({id: id, title: title, message: message, type: type});
  110.             resultSet.next();
  111.         }
  112.        
  113.         resultSet.close();
  114.         database.close();
  115.        
  116.         return result;
  117.     },
  118.    
  119.     getRepeatedNotifications : function(day, hours, mins)
  120.     {
  121.         var database = Titanium.Database.open(this.DATABASE_NAME);
  122.         var resultSet = database.execute("SELECT id, title, message FROM "+this.REPEATED_TABLE+" WHERE (day=? OR day<0) AND hours=? AND minutes=?", day, hours, mins);
  123.         var result = [];
  124.    
  125.         while(resultSet.isValidRow())
  126.         {
  127.             var id = parseInt(resultSet.fieldByName("id"));
  128.             var title = resultSet.fieldByName("title");
  129.             var message = resultSet.fieldByName("message");
  130.             var type = "repeated";
  131.            
  132.             result.push({id: id, title: title, message: message, type: type});
  133.             resultSet.next();
  134.         }
  135.        
  136.         resultSet.close();
  137.         database.close();
  138.        
  139.         return result;
  140.     },
  141.    
  142.     getNotifications : function(date)
  143.     {
  144.         date = date instanceof Date ? date : new Date();
  145.         var year = date.getFullYear();
  146.         var month = date.getMonth();
  147.         var day = date.getDate();
  148.         var hours = date.getHours();
  149.         var mins = date.getMinutes();
  150.        
  151.         var single = this.getSingletimeNotifications(year, month, day, hours, mins);
  152.         var repeated = this.getRepeatedNotifications(day, hours, mins);
  153.        
  154.         return single.concat(repeated);
  155.     },
  156.    
  157.     clearNotification : function(id, type)
  158.     {
  159.         var database = Titanium.Database.open(this.DATABASE_NAME);
  160.         if(type == "repeated")
  161.             database.execute("DELETE FROM "+this.REPEATED_TABLE+" WHERE id=?", id);
  162.         else if(type == "single")
  163.             database.execute("DELETE FROM "+this.SINGLETIME_TABLE+" WHERE id=?", id);
  164.     },
  165.    
  166.     sendNotification: function(data)
  167.     {
  168.         var defaults =
  169.         {
  170.             title: 'notification',
  171.             message: 'message',
  172.             className: 'com.notification.test.NotificationActivity'
  173.         };
  174.        
  175.         data = this.mergeObjects(defaults, data);
  176.        
  177.         var intent = Ti.Android.createIntent
  178.         (
  179.             {
  180.                 flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK,
  181.                 //className : data.className,
  182.             }
  183.         );
  184.        
  185.         intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
  186.        
  187.         var pending = Titanium.Android.createPendingIntent
  188.         (
  189.             {
  190.                 intent : intent,
  191.                 flags : Titanium.Android.FLAG_UPDATE_CURRENT
  192.             }
  193.         );
  194.  
  195.         var notification = Titanium.Android.createNotification
  196.         (
  197.             {
  198.                 contentTitle : data.title,
  199.                 contentText : data.message,
  200.                 contentIntent : pending
  201.             }
  202.         );
  203.  
  204.         Titanium.Android.NotificationManager.notify(1, notification);
  205.     },
  206.    
  207.     sendAllNotifications : function()
  208.     {
  209.         var ns = this.getNotifications();
  210.         for(var i=0; i<ns.length; i++)
  211.         {
  212.             var n = ns[i];
  213.             this.sendNotification
  214.             (
  215.                 {
  216.                     title: n.title,
  217.                     message: n.message,
  218.                 }
  219.             );
  220.            
  221.             if(n.type == "single")
  222.                 this.clearNotification(n.id, n.type);
  223.         }
  224.     }
  225. };
  226.  
  227. module.exports = NotificationHandler;
  228.  
  229. // The contents of NotificationService.js
  230.  
  231. (function()
  232. {
  233.     var NotificationHandler = require('notification/NotificationHandler');
  234.     NotificationHandler.sendAllNotifications();
  235.    
  236.     Ti.API.info(new Date().getSeconds());
  237.    
  238. })();
  239.  
  240. // And to initialize and test it
  241. var NotificationHandler = require('notification/NotificationHandler');
  242. NotificationHandler.initialize();
  243. NotificationHandler.addNotification();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement