I am using Firebase push notifications in my Android App. I can send correctly notification with custom icon, but I have not managed to play my custom sound. I always get the default sound of my device.
{
"registration_ids": "myToken",
"notification": {
"body": "my body",
"title": "my title",
"icon": "ic_notification",
"sound": "mysound.mp3" // I tried "mysound", "mysound.wav"...
},
"priority": "high"
}
The custom sound is located in /res/raw
I have been able to play my custom sound with onMessageReceived
and Firebase data message but not with Firebase notification message.
My android device is Xiaomi Mi A1 and Oreo 8.1., also tried with Xiaomi Mi A2 with same result.
I tried with php and curl, with node.js... always same problem, I get my default sound.
UPDATE
With this code for node.js does not work either:
var registrationToken = 'xxxxxx';
var message = {
notification: {
title: 'my title',
body: 'my body',
},
android: {
ttl: 3600 * 1000,
notification: {
color: '#ff0000',
sound: 'mysound.mp3'
}
},
token: registrationToken
};
Here's how to manually add a custom sound to your settings. Go to Settings > Apps & notifications > Notifications. Scroll down and tap Advanced > Default notification sound. Tap My Sounds.
According to the Firebase documentation, to set the sound that is played, you just need to set the sound parameter of the notification to the filename of the sound to be played; the file must be located in /res/raw/.
Disable Mute or Vibration ModeYou might have accidentally enabled Mute or Vibration mode on your Samsung Galaxy phone and that's why you don't hear notification sounds. To disable those modes, you need to enable Sound mode. For that, go to Settings > Sounds and vibration. Check the box under Sound.
Finally I found the solution. For Android 8.0 and higher it's necessary to create a notification channel in your App:
NotificationChannel channel = new NotificationChannel('my_id', name, importance);
(more info: https://developer.android.com/training/notify-user/channels#java)
Then when you send the notification:
var registrationToken = 'xxxxxx';
var message = {
notification: {
title: 'my title',
body: 'my body',
},
android: {
ttl: 3600 * 1000,
notification: {
color: '#ff0000',
sound: 'mysound.mp3',
channel_id: 'my_id' // important to get custom sound
}
},
token: registrationToken
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With