Home > Android > Android Status-Bar Notifications

Android Status-Bar Notifications

One of the ways to notify the Android user is by displaying notifications in the status bar. You can show a message, play sound add icon and more.

Creating a status bar notification

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "Notification Test", System.currentTimeMillis());
Context context = getApplicationContext();
CharSequence contentTitle = "My notification Title";
CharSequence contentText = "This is the message";
Intent notificationIntent = new Intent(NotificationTest.this, NotificationTest.class);
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "4"); //using sound
notification.vibrate = new long[]{0,100,200,300}; //vibrate (don't forget to add permission)
// led light
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//auto cancel after select
notification.flags |= Notification.FLAG_AUTO_CANCEL;
PendingIntent contentIntent = PendingIntent.getActivity(NotificationTest.this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);

android notifications