/*global Ti: true, alert: true, require: true, setInterval: true, module: true*/ function showNotification(data){ title = data.title ? data.title : '', statusBarMessage = data.message ? data.message : '', message = data.message ? data.message : '', notificationId = (function () { // android notifications ids are int32 // java int32 max value is 2.147.483.647, so we cannot use javascript millis timpestamp // let's make a valid timed based id: // - we're going to use hhmmssDYLX where (DYL=DaysYearLeft, and X=0-9 rounded millis) // - hh always from 00 to 11 // - DYL * 2 when hour is pm // - after all, its max value is 1.159.597.289 var str = '', now = new Date(); var hours = now.getHours(), minutes = now.getMinutes(), seconds = now.getSeconds(); str += (hours > 11 ? hours - 12 : hours) + ''; str += minutes + ''; str += seconds + ''; var start = new Date(now.getFullYear(), 0, 0), diff = now - start, oneDay = 1000 * 60 * 60 * 24, day = Math.floor(diff / oneDay); // day has remaining days til end of the year str += day * (hours > 11 ? 2 : 1); var ml = (now.getMilliseconds() / 100) | 0; str += ml; return str | 0; })(); // create launcher intent var ntfId = Ti.App.Properties.getInt('ntfId', 0), launcherIntent = Ti.Android.createIntent({ className: 'net.iamyellow.gcmjs.GcmjsActivity', action: 'action' + ntfId, // we need an action identifier to be able to track click on notifications packageName: Ti.App.id, flags: Ti.Android.FLAG_ACTIVITY_NEW_TASK | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP }); launcherIntent.addCategory(Ti.Android.CATEGORY_LAUNCHER); launcherIntent.putExtra("ntfId", ntfId); // increase notification id ntfId += 1; Ti.App.Properties.setInt('ntfId', ntfId); // create notification var pintent = Ti.Android.createPendingIntent({ intent: launcherIntent }), notification = Ti.Android.createNotification({ contentIntent: pintent, contentTitle: title, contentText: message, tickerText: statusBarMessage, icon: Ti.App.Android.R.drawable.appicon, flags: Ti.Android.FLAG_AUTO_CANCEL | Ti.Android.FLAG_SHOW_LIGHTS }); Ti.Android.NotificationManager.notify(notificationId, notification); } (function (API) { // **************************************************************************************************************** // **************************************************************************************************************** // private helpers function log(msg) { Ti.API.info('>>> ' + msg); } // **************************************************************************************************************** // **************************************************************************************************************** // module API API.start = function () { function registerToken(token){ if(!Titanium.App.Properties.getString('token')) { Titanium.App.Properties.setString("pendingTokenRegistration", token); return false; } var http = Ti.Network.createHTTPClient(); http.onload = function() { var response = JSON.parse(this.responseText); if (response.success) { if (response.user) { Titanium.App.Properties.setString("pendingTokenRegistration", null); Titanium.App.Properties.setObject("user", response.user); } else { Titanium.App.Properties.setString("pendingTokenRegistration", token); } } else { Titanium.App.Properties.setString("pendingTokenRegistration", token); } }; var url = Titanium.App.Properties.getString("server") + "/registerTokens"+"?access_token="+Titanium.App.Properties.getString('token'); http.open('POST', url); http.send({ deviceuid: Titanium.Platform.id, devicetoken: token, devicetype: 'android', enabled: true }); }; Ti.App.registerToken = registerToken; Ti.App.addEventListener('loggedIn',function(){ Ti.API.log("Logged in received: checking pending registration tokens"); if(Titanium.App.Properties.getString("pendingTokenRegistration")){ registerToken(Titanium.App.Properties.getString("pendingTokenRegistration")); Titanium.App.Properties.setString("pendingTokenRegistration",null); } }); var gcm = require('lib.gcm'); pendingData = gcm.getData(); if (pendingData !== null) { //log('GCM: has pending data on START'); } gcm.doRegistration({ success: function (ev) { //log('GCM success, deviceToken = ' + ev.deviceToken); registerToken(ev.deviceToken); }, error: function (ev) { log('GCM error = ' + ev.error); }, callback: function (data) { //log('GCM notification while in foreground'); Ti.App.Properties.setObject('pendingNotificationData', { title: data.title, statusBarMessage: data.message, message: data.message, data: (data.data ? JSON.parse(data.data) : null) }); showNotification(data); }, unregister: function (ev) { //log('GCM: unregister, deviceToken =' + ev.deviceToken); }, data: function (data) { //log('GCM: has pending data on RESUME'); setTimeout(function(){ var pendingNotificationData = Ti.App.Properties.getObject('pendingNotificationData'); if(pendingNotificationData && pendingNotificationData.data) { Ti.API.log(JSON.stringify(pendingNotificationData.data)); if(pendingNotificationData.data.task&&pendingNotificationData.data.task.task_id) { Ti.App.fireEvent('openNotificationTask',pendingNotificationData.data.task); } Ti.App.Properties.setObject('pendingNotificationData',null); } else { alert('no info'); } },1000); } }); }; })(module.exports);