In the notification payload of the notification there is a sound key.
From the official documentation its use is:
Indicates a sound to play when the device receives a notification.
Supports default or the filename of a sound resource bundled in the
app. Sound files must reside in /res/raw/.
For those who don't know firebase handles notifications differently when the app is in background. In this case the onMessageReceived function is not called.
When your app is in the background, Android directs notification
messages to the system tray. A user tap on the notification opens the
app launcher by default. This includes messages that contain both
notification and data payload. In these cases, the notification is
delivered to the device's system tray, and the data payload is
delivered in the extras of the intent of your launcher Activity.
For downstream messaging, FCM provides two types of payload:
notification and data.
For notification type, FCM automatically displays the message to
end-user devices on behalf of the client app. Notifications have a
predefined set of user-visible keys.
For data type, client app is responsible for processing data messages. Data messages have only custom key-value pairs.
Use notifications when you want FCM to handle displaying a
notification on your client app's behalf. Use data messages when you
want your app to handle the display or process the messages on your
Android client app, or if you want to send messages to iOS devices
when there is a direct FCM connection.
App behaviour when receiving messages that include both notification
and data payloads depends on whether the app is in the background or
the foreground—essentially, whether or not it is active at the time of
receipt. When in the background, apps receive the notification payload
in the notification tray, and only handle the data payload when the
user taps on the notification. When in the foreground, your app receives a message object with both payloads available.
If you are using the firebase console to send notifications, the payload will always contain the notification type. You have to use the Firebase API to send the notification with only the data type in the notification payload. That way your app is always notified when a new notification is received and the app can handle the notification payload.
If you want to play notification sound when app is in background using the conventional method, you need to add the sound parameter to the notification payload.
I am able to play notification sound even if I send it from firebase console. To do that you just need to add key "sound" with value "default" in advance option.
I was also having a problem with notifications that had to emit sound, when the app was in foreground everything worked correctly, however when the app was in the background the sound just didn't come out.
The notification was sent by the server through FCM, that is, the server mounted the JSON of the notification and sent it to FCM, which then sends the notification to the apps. Even if I put the sound tag, the sound does not come out in the backgound.
Even putting the sound tag it didn't work.
After so much searching I found the solution on a github forum. I then noticed that there were two problems in my case:
1 - It was missing to send the channel_id tag, important to work in API level 26+
2 - In the Android application, for this specific case where notifications were being sent directly from the server, I had to configure the channel id in advance, so in my main Activity I had to configure the channel so that Android knew what to do when notification arrived.
This finally solved my problem, I hope it helps someone not to waste 2 days like I did. I don't know if it is necessary for everything I put in the code, but this is the way. I also didn't find the github forum link to credit the answer anymore, because what I did was the same one that was posted there.
You need to make sure your system enable sound notifications for your app, some devices like Xiaomi disable this option by default, so you will not hear notifications sound until you go to:
Settings > Notifications > (search for your app and click on it name)...
Then enable (Allow Sound) option As shown in this picture: