Skip to content

Instantly share code, notes, and snippets.

@jonalter
Created October 2, 2011 22:48
Show Gist options
  • Save jonalter/1258071 to your computer and use it in GitHub Desktop.
Save jonalter/1258071 to your computer and use it in GitHub Desktop.
Android: notification that will launch app
var win = Titanium.UI.createWindow({
backgroundColor: 'blue'
});
var btn = Ti.UI.createButton({
title : 'Add Notification'
});
btn.addEventListener('click', function(e) {
var activity = Ti.Android.currentActivity();
var intent = Ti.Android.createIntent({
action: Ti.Android.ACTION_MAIN,
// className and packageName can be found by looking in the build folder
// for example, mine looked like this
// build/android/gen/com/appcelerator/test/Test7Activity.java
className : 'com.appcelerator.test.Test7Activity',
packageName : 'com.appcelerator.test',
flags : Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP
});
intent.addCategory(Titanium.Android.CATEGORY_LAUNCHER);
var pending = Ti.Android.createPendingIntent({
activity : activity,
intent : intent,
type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
flags : Ti.Android.FLAG_ACTIVITY_NO_HISTORY
});
var ts = new Date().getTime()+5000000;
var notification = Ti.Android.createNotification({
contentIntent : pending,
contentTitle : 'Test',
contentText : 'test',
tickerText : 'This is a test',
// when will only put the timestamp on the notification and nothing else.
// Setting it does not show the notification in the future
when : ts,
icon : Ti.App.Android.R.drawable.appicon,
flags : Titanium.Android.ACTION_DEFAULT | Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS
});
Ti.Android.NotificationManager.notify(1, notification);
});
win.add(btn);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment