Skip to content

Instantly share code, notes, and snippets.

@egomez99
Created November 10, 2012 03:13
Show Gist options
  • Save egomez99/4049694 to your computer and use it in GitHub Desktop.
Save egomez99/4049694 to your computer and use it in GitHub Desktop.
Android Notifications
var win = Titanium.UI.createWindow({
backgroundColor : 'blue'
});
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 : 'ExampleService.js'
});
intent.putExtra('interval', deltaMS);
intent.putExtra('message' , 'This is that little extra');
Ti.Android.startService(intent);
});
win.add(btn);
win.open();
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,
// you can use className or url to launch the app
// 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',
// 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);
}
<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="ExampleService.js"/>
</services>
</android>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment