react-native-notification-sounds
react-native-notification-sounds copied to clipboard
default sound
I need to show the default notification sound which is selected in the device. Is there any way to get the default notification sound.
Did you figure anything out?
how to get default sound of ringtone?
Yes , It is only possible to get default device sound on android. On IOS we don't have acess to device ringtones and alerts.
I had to fork the package and add a function on android to get device default alert sound .
@ReactMethod public void getDefaultSound(final Promise promise) { try { Uri defautNotification = RingtoneManager.getActualDefaultRingtoneUri(this.reactContext, RingtoneManager.TYPE_NOTIFICATION); String result = java.net.URLDecoder.decode(String.valueOf(defautNotification), StandardCharsets.UTF_8.name()); promise.resolve(result); } catch (Exception e) { e.printStackTrace(); } }
the above function will return the url. We will need to format this url to get the name.
The JS code for formatting url is
if (url) { const soundName = url.split('/').pop().split('.')[0]; //Different android OS has different formatting on sound URL, Including a formatting for certain android devices if (soundName.includes('?')) { const formattedName = soundName.substring( soundName.indexOf('=') + 1, soundName.lastIndexOf('&'), ); return formattedName; } return soundName; }
My forked liabrary is [https://github.com/martinattin/react-native-notification-sounds.git]
You can check commit history and make those changes in your liabrary to acheive this functionality.
To play the default sound, just call playSampleSound()
with no parameters. This only works for Android.
To work with the default value you have to pass it like this: playSampleSound([])