How do I add my app's notification sound to the notification sound list?
Asked Answered
G

2

6

I want the user to be able to choose a notification sound for my app so I use the code below:

Intent ringtoneIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false);
ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Choose");
ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
fragment.startActivityForResult(ringtoneIntent, REQUEST_RINGTONE);

Unfortunately, my app's own notification sound isn't in the list. Google Hangouts, Calendar, and Facebook are in the list. I assume these programs have done something to register with android, but I cannot find any documentation on how to do this.

Geosphere answered 23/2, 2015 at 18:42 Comment(0)
S
4

First copy your file to the rigntones folder (See Environment.DIRECTORY_RINGTONES)

Then register the sound:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, nameOfSound);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, yourAppName);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
getContentResolver().insert(uri, values);
Slovenia answered 28/3, 2015 at 8:4 Comment(0)
D
1

One very easy and straightforward solution would be simply copying your sound files to Environment.DIRECTORY_NOTIFICATIONS or Environment.DIRECTORY_RINGTONES.

Dominion answered 27/3, 2015 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.