Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show notification in status bar?

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?

like image 702
Vladimir Avatar asked Dec 16 '12 13:12

Vladimir


People also ask

How do I get notifications on my status bar?

The user can reveal the Notifications window by pulling down the status bar (or selecting Notifications from the Home options menu).

How do I add notification icon to taskbar?

To change how icons and notifications appearPress and hold or right-click any empty space on the taskbar and select Taskbar settings. Under Taskbar corner icons: Select On for any icons you want to see on the taskbar. Select Off for any icons you don't want to see on the taskbar.


2 Answers

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.

like image 147
Frank Avatar answered Oct 15 '22 23:10

Frank


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

Use the Notification.Builder.setSound() method.

You can get the default ringtone like this:

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

and then set it as notification sound:

Notification.Builder(getApplicationContext()).setSound(uri);

and then after you've built your notification you launch it with:

myNotificationManager.notify(myNotificationId, n);
like image 27
onosendai Avatar answered Oct 15 '22 22:10

onosendai