Skip to content

Instantly share code, notes, and snippets.

@egomez99
Created November 10, 2012 03:11
Show Gist options
  • Save egomez99/4049691 to your computer and use it in GitHub Desktop.
Save egomez99/4049691 to your computer and use it in GitHub Desktop.
OnNewIntent - Android Local Notification
var win = Ti.UI.createWindow({fullscreen:false});
//create some simple UI
var textfield = Ti.UI.createTextField({width:300, height:'auto', top:40, left:20, hintText:'enter message'});
var button = Ti.UI.createButton({title:'Send Notification', width:'auto', top:120, left:20, height:'auto'});
var label = Ti.UI.createLabel({width:'auto', top:200, left:20, height:'auto', width:200, text:'Message from intent:'});
var msgLabel = Ti.UI.createLabel({width:'auto', top:240, left:20, height:'auto', width:200, backgroundColor:'lightgrey', text:'---'});
win.add(textfield);
win.add(button);
win.add(label);
win.add(msgLabel);
win.addEventListener("open", function(evt) {
button.addEventListener("click", function(evt) {
//handle the pending intent from the notification bar
win.activity.addEventListener("newIntent", function(evt)
{
var pushObjStr = evt.intent.getStringExtra("pushReceived"); // ***** GET
if (pushObjStr) {
Ti.API.debug("received data: " + pushObjStr);
msgLabel.text = pushObjStr;
} else {
Ti.API.debug("no data received");
msgLabel.text = "no data received";
}
});
Ti.UI.Android.hideSoftKeyboard();
msgLabel.text = "---"; //reset any previous text value
//grab the user input
var message = textfield.value;
if (!message) {
alert('you must provide a message');
return;
} else {
//clear out what was typed
textfield.value = '';
}
//put some custom data into an intent
var intent = Ti.Android.createIntent(
{
action: Ti.Android.ACTION_MAIN,
flags: Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP,
className: "org.appcelerator.titanium.TiActivity",
packageName: Ti.App.id
});
intent.addCategory(Ti.Android.CATEGORY_LAUNCH);
intent.putExtra("pushReceived", message); //***** PUT
//a pending intent to send open an activity when launched
var pending = Ti.Android.createPendingIntent(
{
activity: win.activity,
intent: intent,
//updateCurrentIntent: false,
type: Ti.Android.PENDING_INTENT_FOR_ACTIVITY
});
//put a notification in the notification bar
var notification = Ti.Android.createNotification(
{
contentIntent: pending,
contentTitle: "New Notification",
contentText: "Msg: " + message,
tickerText: "New Notification"
});
Ti.Android.NotificationManager.notify(1, notification);
});
});
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment