How to show notification in status bar?

Vladimir picture Vladimir · Dec 16, 2012 · Viewed 11.9k times · Source

So i have created this notification in my activity

Notification n = new Notification.Builder(getApplicationContext())
    .setContentTitle("New mail from " + sender)
    .setContentText(subject)
    .setSmallIcon(R.drawable.notification)
    .build();

How can i now show it in status/notification bar along with the sound?

Answer

Frank picture Frank · Dec 16, 2012

There is some good documentation at android developer site and have a look at the NotificationManager doc's

Here you go, some code...:

NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, n);

Note that it is also a goo idea to add your intend to the notification...

mBuilder.setContentIntent(resultPendingIntent);

To add sound you can use the Notification.Builder.setSound() method.