'no sound of firebase notifications on marshmallows, but works on oreo

I don't know why when I send a notification to marshmallow, there is no sound, but when I send it to oreo, there is a sound notification. My firebase code is below

     Intent intent = new Intent(this, MainActivity.class);
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
            intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("idata", idata);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        } else {
            Context c = getApplicationContext();
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
            final String CHANNEL_ID = "default";
    // The user-visible name of the channel.
            final String CHANNEL_NAME = "Default";
            NotificationChannel defaultChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

            notificationManager.createNotificationChannel(defaultChannel);

            intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("idata", idata);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(messageBody)
                    .setWhen(System.currentTimeMillis())
                    .setSound(defaultSoundUri)
                    .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setContentIntent(pendingIntent);

            notificationManager.notify("myapp", 0, notificationBuilder.build());
        }

I searched all over the internet, but there were no results for this, I really needed this, please help me, any help would really be appreciated. thanks



Solution 1:[1]

I had the same problem, but the channel id is only used by oreo & higer android versions. If no sound playing in less than oreo, then make sure you have added "sound": "default"//or any custom filename you have in ur res/ folder. I accidently missed it in my firebase payload. Addding sound:"default" solved my issue.

Example:

 const notificationContent = {
      notification: {
        icon: "default",
        priority:"high",
        sound: "default",
        android_channel_id: "myChannelID",
        title: "new update",
      },      
       data: {//Used on onMessage() function
        priority:"high",
        android_channel_id: "myChannelID",
         click_action: "FLUTTER_NOTIFICATION_CLICK",
         title: "new update",
        },
     };

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rajesh Jr.