Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Last active January 26, 2016 17:20
Show Gist options
  • Save MotiurRahman/7696813 to your computer and use it in GitHub Desktop.
Save MotiurRahman/7696813 to your computer and use it in GitHub Desktop.
Adnroid: Open new activity on notification clicked
/* Here create a notification if you click the Add Notification button notification will start and if you
close the app and click the notification the app will start again.
*/
var win = Titanium.UI.createWindow({
backgroundColor : '#000'
});
// Create a Label.
var Test = Ti.UI.createLabel({
text : 'This is notification test apps click add notification',
color : '#FFF',
font : {
fontSize : 20
},
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
top : 20,
});
// Add to the parent view.
win.add(Test);
var btn = Ti.UI.createButton({
title : 'Add Notification'
});
btn.addEventListener('click', function(e) {
var now = new Date().getTime();
var delta = new Date( now + (4 * 1000) );
var deltaMS = delta - now;
var intent = Ti.Android.createServiceIntent({
url : 'Service.js'
});
intent.putExtra('interval', deltaMS);
intent.putExtra('message' , 'This is that little extra');
Ti.Android.startService(intent);
});
win.add(btn);
win.open();
// Main notification service here
if(!Ti.App.Properties.hasProperty('notificationCount')) {
Ti.App.Properties.setInt('notificationCount', 0);
} else {
Ti.App.Properties.removeProperty('notificationCount');
var activity = Ti.Android.currentActivity;
var intent = Ti.Android.createIntent({
action : Ti.Android.ACTION_MAIN,
// if you use url, you need to make some changes to your tiapp.xml
url : 'app.js',
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 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 : new Date().getTime(),
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);
var service = Ti.Android.currentService;
var serviceIntent = service.getIntent();
// this will display that custom extra that we added when we created the intent
// intent.putExtra('message' , 'This is that little extra');
var teststring = serviceIntent.getStringExtra('message');
Ti.API.info('Extra!: ' + teststring);
Ti.Android.stopService(serviceIntent);
}
//Just paste this xml file in tiapp.xml file
<android xmlns:android="http://schemas.android.com/apk/res/android">
<!-- the activities tag must be added if you want to use the url property to launch your app -->
<activities>
<activity url="app.js">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
</activities>
<!-- the services tag must be added so that our service will run -->
<services>
<service type="interval" url="Service.js"/>
</services>
</android>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment