Setting sound for notification
Asked Answered
F

2

4

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

Frowsty answered 28/2, 2011 at 9:4 Comment(0)
B
2

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.

Bog answered 28/2, 2011 at 15:7 Comment(3)
Thank you Marcus. However when I try to get the id of the ringtone by setting an onclick listener for the ringtone preference, I m getting the following errors: 03-01 10:49:59.021: DEBUG/MediaPlayer(308): Couldn't open file on client side, trying server side 03-01 10:49:59.031: ERROR/MediaPlayerService(34): Couldn't open fd for content://settings/system/notification_sound 03-01 10:49:59.031: ERROR/MediaPlayer(308): Unable to to create media player 03-01 10:49:59.041: ERROR/RingtoneManager(308): Failed to open ringtone content://settings/system/notification_soundFrowsty
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, ringtoneId);Frowsty
You can also reference sound resources you bundle with your application: notification.sound = Uri.parse("android.resource://com.company.package/" + soundResourceId);Ribaldry
D
11

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;
Disapproval answered 7/3, 2013 at 9:20 Comment(0)
B
2

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.

Bog answered 28/2, 2011 at 15:7 Comment(3)
Thank you Marcus. However when I try to get the id of the ringtone by setting an onclick listener for the ringtone preference, I m getting the following errors: 03-01 10:49:59.021: DEBUG/MediaPlayer(308): Couldn't open file on client side, trying server side 03-01 10:49:59.031: ERROR/MediaPlayerService(34): Couldn't open fd for content://settings/system/notification_sound 03-01 10:49:59.031: ERROR/MediaPlayer(308): Unable to to create media player 03-01 10:49:59.041: ERROR/RingtoneManager(308): Failed to open ringtone content://settings/system/notification_soundFrowsty
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, ringtoneId);Frowsty
You can also reference sound resources you bundle with your application: notification.sound = Uri.parse("android.resource://com.company.package/" + soundResourceId);Ribaldry

© 2022 - 2024 — McMap. All rights reserved.