No custom sound with Android Firebase Notification

kurtko picture kurtko · Oct 22, 2018 · Viewed 12.8k times · Source

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

};

Answer

kurtko picture kurtko · Jan 22, 2019

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

};