Notification sound on API 26
26,054
Solution 1
You may have created the channel originally with default sound. Once channel is created it cannot be changed. You need to either reinstall the app or create channel with new channel ID.
Solution 2
I used RingtoneManager, and it is work for me. Try thius code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("Notification title");
builder.setContentText("Notification message.");
builder.setSubText("Url link.");
try {
Uri notification = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_ringtone);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
Solution 3
In Oreo you need to create a channel for that. https://developer.android.com/reference/android/app/NotificationChannel.html
Solution 4
The default sound overrides any sound.
You need to put this in your code:
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
Reference:
![Rodrigo Manguinho](https://i.stack.imgur.com/W7TNK.jpg?s=256&g=1)
Author by
Rodrigo Manguinho
Updated on June 04, 2020Comments
-
Rodrigo Manguinho about 4 years
I have a custom mp3 sound that I use on my notifications. It works fine on all devices below API 26. I tried to set the sound on Notification Channel also, but still no work. It plays the default sound.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) .setAutoCancel(true) .setSmallIcon(R.drawable.icon_push) .setColor(ContextCompat.getColor(this, R.color.green)) .setContentTitle(title) .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification)) .setDefaults(Notification.DEFAULT_VIBRATE) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) .setContentText(message); Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) .build(); channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification), audioAttributes); notificationManager.createNotificationChannel(channel); } notificationManager.notify(1, notification);