0. Create new mobile project with Android support.
1. Paste the above code into app.js
2. Compile.
3. Put a value in the field on the window
4. Click the button, we create a pending notification with the data you typed
5. Find the notification in the Android notification bar and click it
6. The app instance alread open is found and the "newintent" event correctly fires
7. The data we get back from the event is correct if this is the first try
8. Do steps 1 - 4 again with a different value in the textfield
var win = Ti.UI.createWindow({navBarHidden:true});
//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) {
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_LAUNCHER);
intent.putExtra("pushReceived", message); //***** PUT
//a pending intent to send open an activity when launched
var pending = Ti.Android.createPendingIntent(
{
activity: Ti.Android.currentActivity,
intent: intent,
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",
icon: Ti.App.Android.R.drawable.statusbaricon
});
Ti.Android.NotificationManager.notify(1, notification);
});
//handle the pending intent from the notification bar
Titanium.Android.currentActivity.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";
}
});
});
win.open();
Testing steps: Run this code instead
NOTE: This code provides a workaround for Ti.Android.currentActivity (which doesn't work as it should atm- it returns the context activity, which is not necessarily the current activity). The workaround uses win.activity instead
Ti.Android.currentActivity will continue to only report the activity that is associated with a current context (url). This is not currently something that can be changed at the platform level without major impact to other areas. Ti.UI.Window.activity will continue to be the suggested mechanism for addressing this type of use case.
Closing bug. Verified fix on: SDK build: 1.9.0.v20120117144634 Runtime: V8, Rhino Titanium Studio, build: 1.0.8.201201131907 OS: Mac OS X Lion (10.7.1) Device: Droid 3 (2.3.4) Note: To test, comment out *updateCurrentIntent* on line 58 from Hieu test code.