如何远程为React Native推送通知设置自定义声音

3

我已经配置了远程通知,并且一切都按照预期工作。现在我想为我的远程通知设置自定义的声音。我尝试了很多方法,但都没有成功。

我正在使用react-native-push-notificationreact-native-firebase

我将自定义声音文件放置在android/app/src/main/res/raw/sound.mp3

我还尝试了soundName:"sound.mp3",但未能成功。

感谢任何有关此事的帮助。谢谢 :)下面是示例代码:

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,
    });
  }````

请分享你的代码。 - Rajan
@Rajan 是服务器端还是客户端? - Aleem
客户端代码。 - Rajan
请看一下 :) - Aleem
1个回答

0

对于 Android,您必须提供一个频道 ID,例如 channelId: "your-channel-id",如果该频道不存在,则可能无法触发通知。

注意:没有频道,通知可能无法正常工作

  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.
  );

在 soundName 选项中,您可以为此通道设置自定义声音名称(使用“default”作为默认声音),并在发送通知时使用通道 ID。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接