react-native-firebase icon indicating copy to clipboard operation
react-native-firebase copied to clipboard

Can't specify a custom notification sound

Open ghost opened this issue 8 years ago • 31 comments

Hi, Thanks for this module

I'm trying to set a custom notification sound to my app. I have an mp3 file that I'm trying to use as the sound.

I tried adding the mp3 file to the xCode project, and then passing the reference to the "setSound" method as a string .setSound('notifSound.mp3')

I also tried passing a "require" function to the method .setSound(require('notifSound.mp3'))

but it didn't workout and the default notification sound was played.

I tried to look for a guide in the documentation, but little was said about custom notifications sound. Any idea on how to make this work?

ghost avatar Jun 05 '18 11:06 ghost

Good evening,

I have the same problem too. Any solution?

Thank you

react: ^16.4.1 react-native: ^0.56.0 react-native-firebase: ^4.3.7

Jonnyboy85 avatar Aug 31 '18 16:08 Jonnyboy85

I have the same problem, can't specify a custom sound, when I try to set it, the notification plays the default sound and then no notification is shown, in the other hand, when I don't try to set a custom sound the notification shows up correctly but playing the default sound. Is there any solution for this? Or maybe downgrading to a previous version? Thank you.

react: 16.3.1 react-native: 0.55.1 react-native-firebase: ^4.3.8

Darking360 avatar Sep 04 '18 13:09 Darking360

Hi, @Darking360 and @Jonnyboy85

I managed to set a custom sound by adding the mp3 file to the Xcode project.

That is by dragging the file and dropping it on the project's Folder in the Project Navigator. You then pass the file's name as a string to the setSound method like this:

notification = new firebase.notifications.Notification() .setSound('glass.mp3')

ghost avatar Sep 05 '18 16:09 ghost

Hi @ahmedXabdeen,

I'm testing notifications with Android Oreo. To make setSound work on the notification, you must first create the channel.

const channel = new firebase.notifications.Android.Channel(name, Desc, firebase.notifications.Android.Importance.High) .setDescription(ChannelName) .setSound(glass.mp3) <--- ... firebase.notifications().android.createChannel(channel);

And then:

const notification = new firebase.notifications.Notification() .setNotificationId(id) .setTitle(title) .setSound(channel.sound); <---

notification .android.setChannelId(channel.channelId) <--- firebase.notifications().displayNotification(notification);

This works.

You can set up multiple channels with different sounds and use them as you like, replacing the method with the following method:

firebase.notifications (). android.createChannel (channel);

with this:

firebase.notifications (). android.createChannels (channelsArray);

Jonnyboy85 avatar Sep 08 '18 13:09 Jonnyboy85

Thank you so much for this feedback! @Jonnyboy85

ghost avatar Sep 08 '18 16:09 ghost

@Jonnyboy85 I try this, but not work to me

const channel = new firebase.notifications.Android.Channel('vtr_channel', 'vtrChannel', firebase.notifications.Android.Importance.High)
        .setDescription('vtrChannel')
        .setSound(sound);

      firebase.notifications().android.createChannel(channel);

      const notification = new firebase.notifications.Notification()
        .setNotificationId(notificationId)
        .setTitle(title)
        .setBody(body)
        .setData(data)
        .setSound(channel.sound);

      notification
        .android.setChannelId(channel.channelId)
        .android.setSmallIcon('notification')
        .android.setColor('red');

      firebase.notifications().displayNotification(notification);

patrickalbani avatar Sep 11 '18 19:09 patrickalbani

Hi @patrickalbani,

is the sound located under the res / raw folder?

Jonnyboy85 avatar Sep 11 '18 19:09 Jonnyboy85

@Jonnyboy85 yes. In older versions worked as expected

patrickalbani avatar Sep 11 '18 19:09 patrickalbani

Hi @patrickalbani,

what version do you have of react-native? I had problems with Android O in versions before 0.56.0.

If you need to upgrade react-native to version ^0.56, I remind you that you need to upgrade babel to version ^7.0.0-beta.47.

Jonnyboy85 avatar Sep 12 '18 07:09 Jonnyboy85

@Jonnyboy85 you save my day, upgrade to 0.56 and worked. Thank you so much

patrickalbani avatar Sep 12 '18 12:09 patrickalbani

i have upgraded to 0.57 and i can't specify a custom sound, just the default sound. Have someone a working example? How should i use the filename in the .sound() method?

what i done:

  1. put a file notif.mp3 in res/raw
  2. use the sound method to specify the sound
async onNotificacion(fcmNotification: FCMNotification) {
    const notification = new firebase.notifications.Notification()
      .setNotificationId(fcmNotification.data.notificacionId)
      .setTitle(fcmNotification.title)
      .setBody(fcmNotification.body)
      .setData(fcmNotification.data)
      .setSound('notif.mp3') // 'notif' 'notif.mp3' or how? it works with 'default'
      .android.setChannelId(FIREBASE.ANDROID_CHANNEL)
      .android.setColor(Colors.appColor)
      .android.setAutoCancel(true)
      .android.setSmallIcon('ic_stat_notf');

    firebase.notifications().displayNotification(notification);
}

dengue8830 avatar Sep 15 '18 10:09 dengue8830

Hi @dengue8830 ,

did you include setSound also in the channel?

const channel = new firebase.notifications.Android.Channel('vtr_channel', 'vtrChannel', firebase.notifications.Android.Importance.High) .setDescription('vtrChannel') .setSound('notif.mp3');

Jonnyboy85 avatar Sep 15 '18 15:09 Jonnyboy85

@Jonnyboy85 i have to try again to be sure, i tried in too many combinations and im not sure right now... i will try. But im using android 5 and 6, channel is mandatory even in that versions?

dengue8830 avatar Sep 16 '18 02:09 dengue8830

Hi @dengue8830 ,

as written in the guide below the creation of the channels starts from Android 8.0 (API level 26): https://developer.android.com/training/notify-user/channels

The steps you need to follow are therefore:

  1. channel creation (with setSound)
  2. creation of the notification (with channel id and setSound)

If you want to create notifications with different sounds, create more channels and associate the id in the notification.

I hope I have been helpful.

Jonnyboy85 avatar Sep 16 '18 11:09 Jonnyboy85

ios scheduleNotification with customSound not work in foreground (it work with default sound). Only work in background. Anyone can help pls?

ducpt2 avatar Sep 27 '18 15:09 ducpt2

Hello 👋, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 25 '18 16:10 stale[bot]

when i send notifications from the firebase panel sounds works, so the problem could be in sender message config. I didn't found a working example with the latest version of this lib and enabled sound

dengue8830 avatar Oct 25 '18 16:10 dengue8830

Hello 👋, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 22 '18 17:11 stale[bot]

Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.

stale[bot] avatar Dec 06 '18 18:12 stale[bot]

I already set two channels with different sound file... but why it wont work as expected...

dominicsavb avatar Dec 27 '18 02:12 dominicsavb