4

I've tried pretty much all available answers on the site, but somehow I cannot get the notification sound to work. The current code I'm testing is this (the notification is built in an alarm reciever):

public class AlarmReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 0;

@Override
public void onReceive(Context context, Intent intent) {


  NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity_.class), 0))
            .setContentText("temporary text")
            .setAutoCancel(true)
            .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                    + "://" + context.getPackageName() + "/raw/alert"))
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_stat_notification);

    Notification notification = builder.build();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, notification);

The sound however, can be played via MediaPlayer, so the format is not the issue here. Could it have to do with the lenght of the sound (30 seconds)? Thank you for your help!

3
  • What is the sound format? I had problems with sounds that can be played via MediaPlayer, but not from notifications... try to replace your sound file by a wmv, for example. (I don't remember exactly the file format I had problems with) Commented Nov 14, 2014 at 14:01
  • I replaced with a wav, and tried removing the raw, not working. Here's a little output, maybe you guys can figure something out if the path is wrong. D/NotificationManager﹕ notify: notification.sound android.resource://*packagename( i have to hide it)*/raw/alert Commented Nov 14, 2014 at 14:14
  • for clarification: R.raw.sound means your sound file sits in res/raw/sound.jpg . And this sound can be reached as R.raw.sound from android Commented Oct 30, 2015 at 10:37

3 Answers 3

6

Try changing your .setSound() to this

.setSound(Uri.parse("android.resource://"
                            + context.getPackageName() + "/"
                            + R.raw.alert))

Hope this will work

Sign up to request clarification or add additional context in comments.

9 Comments

Tried mp3 and wma so far. Do I have to be more specific, I don't know much about codecs etc.
okay, if you think length of the sound can make the difference then try one with a smaller duration,if it works, then duration will be the issue and remember use the same code for .setSound() as i have posted.
Sorry I was away, but the shorter version also doesn't work. (5 seconds). Could that be too much? So far I've eliminated the option that the URI is wrong because I'm using the same one for the media player and it's working.
Well I ended up using a player in my reciever, and set a deleteIntent to handle the stopping of the sound, but your answer helped me get the correct Uri, thank you.
@MukeshRana this code works but if device notification sound is off then it does not increase the volume and finally no notification sound listens
|
2

READ HERE if the correct answer from Mukesh doesn't work for you!!

The answer published from Mukesh is correct, but for me it wasn't working until I discovered I configured the notification wrong!! (or there is an android BUG)

This snippet doesn't work

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( context )
     .setDefaults( DEFAULT_VIBRATE | FLAG_SHOW_LIGHTS )
     .setContentTitle( title )
     .setContentText( message )

     .setSound( "android.resource://"
                        + context.getPackageName() + "/"
                        + R.raw.alert)).build();

-

This snippet WORKS

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( context )
     .setContentTitle( title )
     .setContentText( message )
     .setSound( "android.resource://"
                        + context.getPackageName() + "/"
                        + R.raw.alert)).build();

-

.setDefaults( DEFAULT_VIBRATE | FLAG_SHOW_LIGHTS )

Observation: Like you see the line above I doesn't use the constanst DEFAULT_SOUND, but still does't work.

3 Comments

Thank you!!!!! Was getting super frustrated that it wasn't playing my chosen sound, only the system notification sound. Take the setDefaults off even if set to SOUND, and voila, works perfectly!
@Tano have you tried to play Device stored file path URI above 7.0 (N) and 8.0 (O) getting FileUriExposedException any idea about this
Thats all great but is there any limitation on the length of the notification sound?
0

For release build, it is mandatory to create keep.xml in raw folder. keep.xml with following content will do the job as of Oct, 2024

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@raw/soundFileNameWithoutExtention" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.