简体   繁体   中英

Android Notification custom sound not working

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 :(

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)

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM