Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The NotificationHandler "class"
- var NotificationHandler =
- {
- DATABASE_NAME: "notifications",
- SINGLETIME_TABLE : "singletime",
- REPEATED_TABLE : "repeated",
- SERVICE_PROPERTY: "service_running",
- mergeObjects:function(defaultObj, newObj)
- {
- if(typeof defaultObj != 'object')
- defaultObj = {};
- if(typeof newObj != 'object')
- newObj = {};
- for(var property in newObj)
- if(newObj[property] != null && newObj[property] != undefined)
- defaultObj[property] = newObj[property];
- return defaultObj;
- },
- clear : function()
- {
- var database = Titanium.Database.open(this.DATABASE_NAME);
- database.execute("DROP TABLE IF EXISTS "+this.SINGLETIME_TABLE);
- database.execute("DROP TABLE IF EXISTS "+this.REPEATED_TABLE);
- database.close();
- this.initialize();
- },
- initialize : function()
- {
- var database = Titanium.Database.open(this.DATABASE_NAME);
- 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)");
- 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)");
- database.close();
- var isRunning = Ti.App.Properties.getBool(this.SERVICE_PROPERTY, false);
- Ti.API.info("running: "+isRunning);
- var intent = Titanium.Android.createServiceIntent({url : 'notification/NotificationService.js' });
- intent.putExtra('interval', 1000);
- Titanium.Android.stopService(intent);
- var service = Titanium.Android.createService(intent);
- service.addEventListener('start', function()
- {
- Ti.API.info("Started");
- Ti.App.Properties.setBool(NotificationHandler.SERVICE_PROPERTY, true);
- });
- service.addEventListener('stop', function()
- {
- Ti.API.info("Stoped");
- Ti.App.Properties.setBool(NotificationHandler.SERVICE_PROPERTY, false);
- });
- service.start();
- },
- addNotification: function(data)
- {
- var defaults =
- {
- title: 'Notification',
- message: 'Message',
- singleTime: new Date().getTime()+(1*60*1000),
- repeatDay: -1,
- repeatHour: 12,
- repeatMinute: 0,
- repeatWeekly: false,
- };
- data = this.mergeObjects(defaults, data);
- var database = Titanium.Database.open(this.DATABASE_NAME);
- if(data.repeatWeekly)
- {
- database.execute("INSERT INTO "+this.REPEATED_TABLE+" VALUES(?,?,?,?,?,?)", null, data.title, data.message, data.repeatDay, data.repeatHour, data.repeatMinute);
- }
- else
- {
- var date = new Date(data.singleTime)
- var year = date.getFullYear();
- var month = date.getMonth();
- var day = date.getDate();
- var hours = date.getHours();
- var mins = date.getMinutes();
- database.execute("INSERT INTO "+this.SINGLETIME_TABLE+" VALUES(?,?,?,?,?,?,?,?)", null, data.title, data.message, year, month, day, hours, mins);
- }
- },
- getSingletimeNotifications : function(year, month, day, hours, mins)
- {
- var database = Titanium.Database.open(this.DATABASE_NAME);
- 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);
- var result = [];
- while(resultSet.isValidRow())
- {
- var id = parseInt(resultSet.fieldByName("id"));
- var title = resultSet.fieldByName("title");
- var message = resultSet.fieldByName("message");
- var type = "single";
- result.push({id: id, title: title, message: message, type: type});
- resultSet.next();
- }
- resultSet.close();
- database.close();
- return result;
- },
- getRepeatedNotifications : function(day, hours, mins)
- {
- var database = Titanium.Database.open(this.DATABASE_NAME);
- var resultSet = database.execute("SELECT id, title, message FROM "+this.REPEATED_TABLE+" WHERE (day=? OR day<0) AND hours=? AND minutes=?", day, hours, mins);
- var result = [];
- while(resultSet.isValidRow())
- {
- var id = parseInt(resultSet.fieldByName("id"));
- var title = resultSet.fieldByName("title");
- var message = resultSet.fieldByName("message");
- var type = "repeated";
- result.push({id: id, title: title, message: message, type: type});
- resultSet.next();
- }
- resultSet.close();
- database.close();
- return result;
- },
- getNotifications : function(date)
- {
- date = date instanceof Date ? date : new Date();
- var year = date.getFullYear();
- var month = date.getMonth();
- var day = date.getDate();
- var hours = date.getHours();
- var mins = date.getMinutes();
- var single = this.getSingletimeNotifications(year, month, day, hours, mins);
- var repeated = this.getRepeatedNotifications(day, hours, mins);
- return single.concat(repeated);
- },
- clearNotification : function(id, type)
- {
- var database = Titanium.Database.open(this.DATABASE_NAME);
- if(type == "repeated")
- database.execute("DELETE FROM "+this.REPEATED_TABLE+" WHERE id=?", id);
- else if(type == "single")
- database.execute("DELETE FROM "+this.SINGLETIME_TABLE+" WHERE id=?", id);
- },
- sendNotification: function(data)
- {
- var defaults =
- {
- title: 'notification',
- message: 'message',
- className: 'com.notification.test.NotificationActivity'
- };
- data = this.mergeObjects(defaults, data);
- var intent = Ti.Android.createIntent
- (
- {
- flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK,
- //className : data.className,
- }
- );
- intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
- var pending = Titanium.Android.createPendingIntent
- (
- {
- intent : intent,
- flags : Titanium.Android.FLAG_UPDATE_CURRENT
- }
- );
- var notification = Titanium.Android.createNotification
- (
- {
- contentTitle : data.title,
- contentText : data.message,
- contentIntent : pending
- }
- );
- Titanium.Android.NotificationManager.notify(1, notification);
- },
- sendAllNotifications : function()
- {
- var ns = this.getNotifications();
- for(var i=0; i<ns.length; i++)
- {
- var n = ns[i];
- this.sendNotification
- (
- {
- title: n.title,
- message: n.message,
- }
- );
- if(n.type == "single")
- this.clearNotification(n.id, n.type);
- }
- }
- };
- module.exports = NotificationHandler;
- // The contents of NotificationService.js
- (function()
- {
- var NotificationHandler = require('notification/NotificationHandler');
- NotificationHandler.sendAllNotifications();
- Ti.API.info(new Date().getSeconds());
- })();
- // And to initialize and test it
- var NotificationHandler = require('notification/NotificationHandler');
- NotificationHandler.initialize();
- NotificationHandler.addNotification();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement