Generate StatusBar Notification in Android via Code with custom Sound,Vibrate and Lights

by Sasikumar 2013-10-29 14:29:30

To generate status bar notification in android with custom sound,vibrate and lights:
We can set custom sound,vibration and flashing lights using the following coding,
// Create a reference to the NotificationManager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

// Set icon,title and time for the Notification
int icon = R.drawable.notification_icon;
CharSequence tickerText = "MyTickerText";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);

// Set the Notification's message content and Intent
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Title";
CharSequence contentText = "Message of the Notification";
Intent notificationIntent = new Intent(this, YourClassForNotification.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

// Handle the Notification to the NotificationManager
private static final int NOTIFICATION_ID = 1; // this ID can be any integer to identify the notification uniqly
mNotificationManager.notify(NOTIFICATION_ID, notification);


// Step to add Sound to notification
// 1st method - to add Default sound
notification.defaults |= Notification.DEFAULT_SOUND;
( or )
// 2nd method - to add Custom sound from SD card
notification.sound = Uri.parse("file:///sdcard/mediafiles/mysong.mp3");


// Step to add Vibration to notification
// 1st method - to add Default vibration
notification.defaults |= Notification.DEFAULT_VIBRATE;
( or )
// 2nd method - to add a Custom vibration
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;


// Step to add flash-lights to notification
// 1st method - to add Default lights
notification.defaults |= Notification.DEFAULT_LIGHTS;
( or )
// 2nd method - to add Custom lights
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

You can use either 1st method or 2nd method as needed to add Sound, Vibration and Flash-lights.
807
like
0
dislike
0
mail
flag

You must LOGIN to add comments