Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting sound for notification

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..

like image 683
Dijo David Avatar asked Dec 13 '22 15:12

Dijo David


2 Answers

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;
like image 60
David Manpearl Avatar answered Jan 14 '23 19:01

David Manpearl


http://developer.android.com/reference/android/app/Notification.Builder.html#setSound(android.net.Uri)

Notification.Builder.setSound();

Use a ringtone preference in the preference activity to get the URI of the selected sound.

like image 40
AverageMarcus Avatar answered Jan 14 '23 18:01

AverageMarcus