Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification.Builder.setSound has no effect

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.

like image 869
Oliver Salzburg Avatar asked Jul 20 '26 14:07

Oliver Salzburg


1 Answers

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.

like image 61
Oliver Salzburg Avatar answered Jul 22 '26 03:07

Oliver Salzburg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!