RajPatel9490
RajPatel9490

Reputation: 11

PushNotification setSound is not working properly for android os 8

I am using react native firebase library for push notification and i am playing two different sound for two different notification so i am playing some .mp3 sound for one notification and default for other one so problem is app is playing only that sound which is coming in first notification for app and for rest notification playing the first played sound so I think the issue is notification information is not updating that's what it is playing the same sound for all the notification which app got for first notification.even we are getting right information in notification data but it is not updating the sound.

Version: react-native-firebase:"4.3.8" react-native:"0.56.1"

yes I am getting data from firebase and below is my code to set Sound for notification.

this.notificationListener = firebase
  .notifications()
  .onNotification((notification: Notification) => {

    const channel = new firebase.notifications.Android.Channel(
      'test-channel',
      'Test Channel',
      firebase.notifications.Android.Importance.Max
    ).setDescription('My apps test channel');

    if (notification && notification.data) {
      const data = notification.data;
      if (data && data.messageKey) {
        //here I set the sound on basis of notification data to the channel

    ...
      } 
    }

    // Create the channel
    firebase.notifications().android.createChannel(channel);
    // Process your notification as required
    notification
    .android.setChannelId('test-channel')
    .android.setSmallIcon(Images.logoSmall);


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

Upvotes: 0

Views: 2050

Answers (2)

Vishal Dhanotiya
Vishal Dhanotiya

Reputation: 2638

1) In android, add your custom sound file to [project_root]/android/app/src/main/res/raw here

2) Create notification channel

const channel = new firebase.notifications.Android.Channel('channel_name', 'channel_name', firebase.notifications.Android.Importance.High)
  .setDescription('channel_name')

3) Add sound into notification .setSound('default')

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

const localNotification = new firebase.notifications.Notification({
  sound: 'default',
  show_in_foreground: true,
})
  .setNotificationId(new Date().valueOf().toString())
  .setTitle(noti_payload.title)
  .setSound('default')
  .setBody(noti_payload.message)
  .setData({
    now: new Date().toISOString(),
    payload: noti_payload,
  })
  .android.setAutoCancel(true)
  .android.setBigText(noti_payload.message)
  .android.setLargeIcon('ic_launchers')
  .android.setVibrate(1000)
  .android.setColor('#74c900')
  .android.setColorized(true)
  .android.setChannelId('channel_name') // e.g. the id you chose above
  .android.setSmallIcon('ic_launchers') // create this icon in Android Studio
  .android.setPriority(firebase.notifications.Android.Priority.High);

firebase
  .notifications()
  .displayNotification(localNotification)

Important Note:-

After doing above step please uninstall app and delete bundles, because some time cached bundle asset contain default sound and changes are not reflected.

every time you change sound you need to build bundle again

Please check following link also https://rnfirebase.io/docs/v4.0.x/notifications/reference/Notification

Upvotes: 1

Saurabh Thorat
Saurabh Thorat

Reputation: 20764

setSound() was deprecated in API 26. Use NotificationChannel.setSound() instead.

Upvotes: 0

Related Questions