Setting sound for notification

Dijo David picture Dijo David · Feb 28, 2011 · Viewed 16k times · Source

How can I set sound for notification for my android application. In my application notification will be shown after 30 seconds. I want to give options for this alerts such as silent mode, vibration mode and an option to select from the available tones from the device. I am using the preference screen to show the settings menu. I want to to set the notification ring type application specific. Is there any way to establish this..

Answer

David Manpearl picture David Manpearl · Mar 7, 2013

How to Set a Custom Sound for an Android Notification

Place a .mp3 or .wav file in your res/raw directory such as "notification_sound.mp3" as in the example below (the filename must not use capital letters).

Set the Notification.sound when you create your Notification, like this:

final int iconResId = R.drawable.my_icon;
final int soundResId = R.raw.notification_sound;
final Notification notification =
    new Notification(iconResId, tickerText, System.currentTimeMillis());
final String packageName = context.getPackageName();
notification.sound =
    Uri.parse("android.resource://" + packageName + "/" + soundResId);

Optionally, add vibration to your Notification:

notification.defaults = Notification.DEFAULT_VIBRATE;