Created
September 30, 2014 14:29
-
-
Save rpavez/fa40904c20ca5c1fa612 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global Ti: true, require: true */ | |
(function(service) { | |
var serviceIntent = service.getIntent(), title = serviceIntent.hasExtra('title') ? serviceIntent.getStringExtra('title') : '', statusBarMessage = serviceIntent.hasExtra('message') ? serviceIntent.getStringExtra('message') : '', message = serviceIntent.hasExtra('message') ? serviceIntent.getStringExtra('message') : ''; | |
Ti.App.Properties.setObject('pendingNotificationData', { | |
title : title, | |
statusBarMessage : statusBarMessage, | |
message : message, | |
data : (serviceIntent.hasExtra('data') ? JSON.parse(serviceIntent.getStringExtra('data')) : null) | |
}); | |
var 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); | |
service.stop(); | |
})(Ti.Android.currentService); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global Ti: true, require: true */ | |
(function(activity, gcm) { | |
var intent = activity.intent; | |
// HERE we catch the intent extras of our notifications | |
if (intent.hasExtra('ntfId')) { | |
// and then we'll use 'data' property to pass info to the app (see pendingData lines of the 1st snippet) | |
gcm.data = { | |
ntfId : intent.getIntExtra('ntfId', 0) | |
}; | |
} | |
// 'isLauncherActivity' is a module property which tell us if the app is not running | |
if (gcm.isLauncherActivity) { | |
// if the app is not running, we need to start our app launcher activity | |
// (launcher activity shows the splash screen and setup your app environment, so we need this) | |
var mainActivityIntent = Ti.Android.createIntent({ | |
// 'mainActivityClassName' is another module property with name of our app launcher activity | |
className : gcm.mainActivityClassName, | |
packageName : Ti.App.id, | |
flags : Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP | |
}); | |
mainActivityIntent.addCategory(Ti.Android.CATEGORY_LAUNCHER); | |
activity.startActivity(mainActivityIntent); | |
} else { | |
// if the app is running (is being resumed), just finish this activity! | |
activity.finish(); | |
} | |
})(Ti.Android.currentActivity, require('net.iamyellow.gcmjs')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global Ti: true, alert: true, require: true, setInterval: true, module: true */ | |
(function (API) { | |
var gcm = require('net.iamyellow.gcmjs'); | |
API.doRegistration = function (callbacks) { | |
gcm.registerForPushNotifications(callbacks); | |
}; | |
API.doUnregistration = function () { | |
gcm.unregisterForPushNotifications(); | |
}; | |
API.getData = function () { | |
var data = gcm.data; | |
return !data ? null : data; | |
}; | |
API.setData = function (data) { | |
gcm.data = data; | |
}; | |
})(module.exports); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rpavez - I tried adding gcm.js and lib.gcm.js in module folder with gcm.js and gcm_activity.js under resources folder. But still facing the same issue - i.e. When application is closed (Not in background) and when notification is received, on clicking on it opens splash screen and nothing happens. Any other solution would be helpful?
Also I am unable to figure out the usage of module folder? Any help would be appreciated