Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No custom sound with Android Firebase Notification

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

};
like image 474
kurtko Avatar asked Oct 22 '18 12:10

kurtko


People also ask

Can you get custom notification sounds on Android?

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.

How do I add sound to FCM notification?

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/.

Why do my notifications have no sound?

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.


1 Answers

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

};
like image 101
kurtko Avatar answered Oct 20 '22 01:10

kurtko