React-native-firebase: Set custom sound in notification

Created on 13 Sep 2019  路  10Comments  路  Source: invertase/react-native-firebase

I'm trying to use setSound ('notification.mp3'), but it doesn't work to put a custom notification sound in react native firebase 5.4.0, how can I do this?

*

Already tried to put a require ('path'),
but it doesn't work.

Most helpful comment

This isn't very well documented sorry.

It needs to be inside of a res/raw directory inside of your Android build. It's not possible to provide a sound from a JS file.

All 10 comments

This isn't very well documented sorry.

It needs to be inside of a res/raw directory inside of your Android build. It's not possible to provide a sound from a JS file.

Putting my sound file in res/raw it works? i'll try

By keeping the file in res/raw, it will work when app is in foreground. But if app is in background, app is playing default notification sound. Tried sending sound parameter with custom sound name. Still it is playing default (only in background state).

If your android channel id is the same (AndroidManifest.xml => default_notification_channel_id and notif.android.setChannelId('Test Notification')), in both ways(in the background and in foreground) custom notification tone will be work.

@kalpesh9896 can you elaborate your answer. Its hard to understand. I am able to play custom sound with Notification data that when app is in background, But its not playing when app is in background

@kalpesh9896 can you elaborate your answer. Its hard to understand. I am able to play custom sound with Notification data that when app is in background, But its not playing when app is in background

Hey, how you make it work in background? i'm using the android 9

This isn't very well documented sorry.

It needs to be inside of a res/raw directory inside of your Android build. It's not possible to provide a sound from a JS file.

Hello! Where exactly should be this "res/raw " directory?cd android/app/build?

If you open your projects android directory with Android studio, right click the app directory and add a new resource:

image

Needs a new directory adding with the file in it.

@ankitbaid11326 @JohnVidal77 Did you succeed making sound working in background ?

@meliodev and others
I can confirm it works,

place your sound file, e.g. mycustomsound.ogg, here:

android/app/src/main/res/raw/mycustomsound.ogg

(probably you need to create the raw directory)

Then create a notificaiton:

        const DELIVERY_NOTIFICATIONS_CHANNEL = new firebase.notifications.Android.Channel('MY_CHANNEL_ID', 'Channel name', firebase.notifications.Android.Importance.Max)
            .setDescription('Notifications with available deliveries')
            .enableVibration(true)
            .enableLights(true)
            .setSound("mycustomsound.ogg");

        await firebase.notifications().android.createChannel(DELIVERY_NOTIFICATIONS_CHANNEL);
        const localNotification = new firebase.notifications.Notification()
            .setNotificationId("MY_ NOTIFICATION_ID")
            .setTitle("any title")
            .setBody("any body")
            .setSound("mycustomsound.ogg")
            .android.setBigText("any body")
            .android.setAutoCancel(true)
            .android.setSmallIcon('@drawable/ic_stat_ic_notification')
            .android.setLargeIcon('@drawable/ic_stat_ic_notification')
            .android.setColor('#006064')
            .android.setChannelId(DELIVERY_NOTIFICATIONS_CHANNEL.channelId);
        await firebase.notifications().displayNotification(localNotification);
Was this page helpful?
0 / 5 - 0 ratings