I'm trying to play a sound when receiving a push notification. The gist of the code looks like this:
Resources r = getResources();
int resourceId = r.getIdentifier(soundName, "raw", context.getPackageName());
Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + resourceId);
mBuilder.setSound(soundUri);
However, only the default notification sound is played when the notification is received.
If I play the sound with MediaPlayer, it plays just fine. So I must assume that the URI is correct.
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, resourceId);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
However, I would prefer to solve this through the notification instance itself instead of playing the sound manually.
Somewhere else in the code, we were using mBuilder.setDefaults(DEFAULT_ALL), which implies DEFAULT_SOUND. The documentation for which notes:
Use the default notification sound. This will ignore any given sound.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With