2

I've searched the web and found some post related to setting sound on android notification and followed them but in my case I am getting default notification sound on android notification. Here's my code:

builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification_message));

I've also followed this post, but no luck.

For your information: I've played the URI using MediaPlayer and it's working fine then.

Part of my code:

NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext);
builder.setContentTitle(message.notificationTitle);
builder.setContentText(message.notificationSubtitle);
builder.setTicker(message.notificationTitle);
builder.setSmallIcon(R.drawable.ic_action_inbox);
Uri uri = Uri.parse("android.resource://" + appContext.getPackageName() + "/" + R.raw.notification_message);
builder.setSound(uri);
builder.setVibrate(new long[]{1000, 1000, 1000});
builder.build();

Can anyone explain what's the issue. How to do it? I'm stuck :(

1
  • Check your log to see if there's any exception message. Commented Mar 3, 2016 at 13:41

2 Answers 2

1

Sorry folks. It was my mistake. I used following line:

builder.setDefaults(Notification.DEFAULT_ALL);

which created the problem. commenting the line fixed it.

Thanks for your answers :)

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

Comments

0

Use Context.getPackageName() method to get the package name of Application.

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

remove

Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification_message)

2 Comments

do you think I haven't checked the getpackageName() method. It's just working fine.
My URI is correct. I have played the sound in the URI using Mediaplayer. But it's not working when setting builder.setSound().

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.