简体   繁体   中英

How to set a custom sound in react-native-push-notifications remotely

I have configured remote notifications, and everything is working as expected. Now I want to set a custom sound to my remote notifications. I have tried many ways but these are not working. I am using react-native-push-notification and react-native-firebase .

I have placed a custom sound file in android/app/src/main/res/raw/sound.mp3 .

I have also tried soundName:"sound.mp3" I appreciate any help in this regard. Thanks. :) Below is the sample Code:

PushNotification.configure({
      onRegister: async function(token) {

      },
      onNotification: function(notification) {
        console.log(notification);
      },
      senderID: 'xxxx',
      permissions: {
        alert: true,
        badge: true,
        sound: true,
      },
      playSound: true,
      soundName: 'sound.mp3',
      popInitialNotification: true,
      requestPermissions: true,
    });
  }````

For android you must provide a channel id with channelId: "your-channel-id" , if the channel doesn't exist the notification might not be triggered.

NOTE: Without channel, notifications might not work

  PushNotification.createChannel(
    {
      channelId: "channel-id", // (required)
      channelName: "My channel", // (required)
      soundName: "sound.mp3",
      importance: 4,
      vibrate: true,
    },
    (created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
  );

In the soundName option you put your custom sound name for this channel (for default sound, use 'default'), and use the channel Id when sending a notification

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM