How to show notification in status bar?
Asked Answered
P

2

6

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?

Procurable answered 16/12, 2012 at 13:19 Comment(0)
H
5

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.

Hollis answered 16/12, 2012 at 13:35 Comment(6)
Is it necessary to add the intent? I mean will it start withouth it when i start the activity?Procurable
and it what's exatcly mId, i mean which value should i assign to it because it shows error this way?Procurable
I never did try without it, but i don't think it will start the activity if you don't add it.Hollis
you can assign any int, you can use this int to update the notification later onHollis
i have another problem, this seems to be compatitible with 11 and above, and i need it to work on 2.2. Is there another way to notify the user along with the soundProcurable
@Vladimir: Use NotificationCompat.Builder from the Android Support package, in place of Notification.Builder.Twosome
H
2

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);
Hightail answered 16/12, 2012 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.