Custom Notification Sound not working in Oreo

how to set notification sound in android programmatically in oreo
notification sound not working android programmatically
android oreo notification sounds not working programmatically
android notification channel sound not working
notification icon not showing android oreo
notification sound not working in android studio
android notification sound not working
android oreo default notification sound
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
            mBuilder.setSound(sound);

I had copied the mp3 (notification_mp3.mp3) file into the raw folder in the res folder. When the notification is triggered it produces given mp3 sound upto Android Nougat but default sound in Android Oreo. I had referred many sites but nothing worked on Android Oreo. I didn't find any changes in Android Docs regarding notification sound in Android O & above. What changes should be done to make this code working in Android O too?

To set a sound to notifications in Oreo, you must set sound on NotificationChannel and not on Notification Builder itself. You can do this as follows

Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel mChannel = new NotificationChannel("YOUR_CHANNEL_ID",
            "YOUR CHANNEL NAME",
            NotificationManager.IMPORTANCE_DEFAULT)

        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();

        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, 
                context.getString(R.string.app_name),
                NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel.
        mChannel.setDescription(msg);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setSound(sound, attributes); // This is IMPORTANT


        if (mNotificationManager != null)
            mNotificationManager.createNotificationChannel(mChannel);
    }

This will set a custom sound to your notifications. But if the app is being updated and the notification channel is used before, it won't be updated. i.e. you need to create a different channel and set sound to it to make it work. But this will show multiple channels in the notifications section of app info of your app. If you are setting sound to an entirely new channel that is fine, but if you want the channel being used before, you have to delete the existing channel and recreate the channel. To do that you can do something like that before creating channel

if (mNotificationManager != null) {
            List<NotificationChannel> channelList = mNotificationManager.getNotificationChannels();

            for (int i = 0; channelList != null && i < channelList.size(); i++) {
                mNotificationManager.deleteNotificationChannel(channelList.get(i).getId());
            }
        }

Custom Notification Sound not working in Android 8.x.x (OREO , Reproduce Scenario (including but not limited to). Every time I send notification with sound property, in Android Oreo, it plays the default message  Custom Notification Sound is not working at all in Android 8.x.x Reproduce Scenario (including but not limited to) Every time I send notification with sound property, in Android Oreo, it plays the default message tone.

Create a channel (I use this method in Application.clss to create the channel )

  public void initChannels(Context context) {
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel("default"/*CHANNEL ID*/,
            "CHANNEL_NAME",
            NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Channel description");
    assert notificationManager != null;
    notificationManager.createNotificationChannel(channel);
}

And use this channel default while creating instance of NotificationCompat

 .... notificationBuilder = new NotificationCompat.Builder(this,"default") ....

Custom Sound not playing in Android version 8 (Oreo). Plays default , Palying custom sound files for notifications does not work any more. It worked with plugin-local-notifications V 0.8 and android target version 23. Android Oreo comes with some pretty cool stock ringtones and sounds. However, users who like to personalize their device, have the option of adding their own tracks and sounds. The good news is that adding custom ringtones and notification or alarm sounds isn't all that complicated, especially with Android's easy-to-follow folder system.

Android O comes with NotificationChannel use that instead

 int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            mNotificationManager.createNotificationChannel(notificationChannel);

Custom Notification Sounds for Android Oreo and Beyond, Custom Notification Sounds for Android Oreo and Beyond when they stop playing, and when system settings cause them to not play at all  Android Oreo also gives users more control over notifications than ever before, as in Android 8.0 users can modify the settings for any notification channel that’s present on their device.

try this:

/**
 * show notification
 *
 * @param message
 */
private static void showNotification(RemoteMessage message, Context baseContext) {
    Context context = baseContext.getApplicationContext();
    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context.getApplicationContext());
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, null)
            .setSmallIcon(R.drawable.ic_logo_xxxdpi)
            .setContentTitle(message.getData().get(TITLE))
            .setContentText(message.getData().get(BODY))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
            .setVibrate(new long[]{500, 500})
            .setLights(Color.RED, 3000, 3000)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(getPendingIntent(context, message));
    managerCompat.notify(getRandom(), builder.build());
}

Customize Notification Sounds on Android Oreo, We'll also show how to fix a ringtone for a specific contact. For this How-to tutorial, we used a Google Pixel running Android 8.1.0. Geofence notification not working in android oreo (intent service) 0. Android version 8.1 not display notification on status bar. Android Notification Sound. 795.

Notification sound NOT working after Oreo updates, I tried reboot, power on/off but notification sound would not work. I set ringtone volume to full and slider is on Ring Plz take on high priority as it  The problem with this solution is that users cannot mute the notification sound. It always plays the sound. NotificationCompat.Builder(Context) is depreacted, notifications must specify a NotificationChannel Id using NotificationCompat.Builder(context, String). So it's not the best solution for Oreo / API 26.

Can't set custom notification sound (Android Oreo), While I've set the notification sound in the SmartThings (Classic) app to example) don't seem to have a problem pushing their own sounds,  If we tap “Travel times for major delays,” say, we can then give that notification a custom sound or turn on vibration, give it the privilege to override Do Not Disturb mode, change how it appears

No more separate notification tones in Oreo?, This will present you with the option to assign a ringtone to this conversation. If you decide to use Messages as a work-around, you'll also want to Tap Default Apps -> Messaging app, and change the default to Android Messages. contact-​specific notification tones from Android 8.0 (Oreo), so it isn't  In our testing, we observed that there were three ways of stopping a notification sound once it has been started: clicking it, swiping it, and invoking an action on it. (Unfortunately, twisting it and bopping it didn’t have any effect.) It was quite simple to stop the sound in response to a notification click or action.

Comments
  • You set the sound on the notification channel, not on the notification, in Android 8.0+.
  • it's working perfectly, but only when app is in foreground. I stil have the default sound when app is in backgroun(for oreo). Do you have a solution for that?
  • @NajouaMahi Are you using Firebase Push Notifications?
  • yes i'm using Firebase, they offer meta-data for default notification icon and color if none of them exists, but I didn't find a meta-data for sound.Can you help please!
  • Have a look at this answer stackoverflow.com/a/37845174/2577750
  • Yes we need to send data messages, thank you Ankush, that helped
  • Where is mNotificationManager and mNotificationManager declarations?
  • This is not will work, because you sould use notification channels.