首页 > 解决方案 > Firebase 自定义通知声音未播放

问题描述

我正在使用 Firebase Cloud Messaging,使用 react-native-push-notification 将通知从 mongoDB 发送到 react-native 应用程序。

通知正在工作,但我无法播放我在 /res/raw 文件夹中的 android 应用程序中添加的自定义通知。

我的声音

我的代码:

const message = {
    "to": userFinded.deviceToken,
    "notification": {
      "title": 'Body test',
      "color": '#ff9d00',
      "sound": 'my_sound', //Not working
    },
  };

FCM 文档

我还尝试遵循react-native-push-notification 的文档并这样做:

PushNotification.localNotification({
    /* Android Only Properties */
    channelId: 'fcm_fallback_notification_channel', // (required) channelId, if the channel doesn't exist, notification will not trigger.

    soundName: 'my_sound.mp3', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
  });

  PushNotification.createChannel(
    {
      channelId: 'fcm_fallback_notification_channel', // (required)
      channelName: 'My channel', // (required)
      soundName: 'my_sound.mp3', // (optional) See `soundName` parameter of `localNotification` function
    });

我尝试了不同的版本:

soundName: 'my_sound.mp3';
soundName: 'my_sound';
soundName: 'android.resource://com.xyz/raw/my_sound';
soundName: 'android.resource://com.seeed/raw/my_sound';

有时通知声音是默认的,有时我没有任何声音。

请问有什么帮助吗?

标签: androidreact-nativepush-notificationfirebase-cloud-messagingreact-native-push-notification

解决方案


推荐阅读