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?

Vladimir
  • 1,243
  • 5
  • 19
  • 23

2 Answers2

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.

Frank
  • 16,476
  • 7
  • 38
  • 51
  • Is it necessary to add the intent? I mean will it start withouth it when i start the activity? – Vladimir Dec 16 '12 at 13:51
  • and it what's exatcly mId, i mean which value should i assign to it because it shows error this way? – Vladimir Dec 16 '12 at 13:54
  • I never did try without it, but i don't think it will start the activity if you don't add it. – Frank Dec 16 '12 at 13:54
  • you can assign any int, you can use this int to update the notification later on – Frank Dec 16 '12 at 14:05
  • 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 sound – Vladimir Dec 16 '12 at 14:09
  • @Vladimir: Use `NotificationCompat.Builder` from the Android Support package, in place of `Notification.Builder`. – CommonsWare Dec 16 '12 at 14:11
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);
Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71