【问题标题】:react-native-firebase play custom sound on notification arrivedreact-native-firebase 在通知到达时播放自定义声音
【发布时间】:2020-01-22 14:56:28
【问题描述】:

我有一个 react native 应用程序,它是一个基于 react hooks 的应用程序,我想根据通知负载数据播放自定义音乐。它只在应用打开时工作,但我也想在应用处于后台时播放自定义声音。

useEffect(() => {
    checkFirebase();

    const firebaseNotification = firebase.notifications().onNotification((notification) => {

      console.log("notitication", notification);
      const { sound } = notification.data;
      console.log("sound", sound);
      playSound(sound);
    })

    return () => {
      messageListener();
      firebaseNotification();
      messageCheck();
    }
  })

分享了我的反应原生代码。还分享我从我的 Node.js 应用程序发送的正文

const message = {
        notification: {
            title: "Account Deposit",
            body: "A deposit to your savings account has just cleared.",
        },
        android: {
            ttl: 3600 * 1000,
            notification: {
              color: '#ff0000',
              sound: 'beep.wav'
            }
          },
        data: {
            sound: "beep.wav"
        },
        token
    };
   admin.messaging().send(message)
        .then((response) => {
            // Response is a message ID string.
            console.log('Successfully sent message:', response);
        })
        .catch((error) => {
            console.log('Error sending message:', error);
        });

【问题讨论】:

    标签: firebase react-native react-native-firebase


    【解决方案1】:

    您需要在您的应用程序中添加一些可以让您在应用程序没有专注的情况下进行游戏的东西。我发现react-native-track-player 非常好,应该可以满足您的需求。

    【讨论】:

    • 您建议的是一个可以播放声音的库,我可以使用 react-native-audio 来完成。但我想知道什么时候有 firebase 通知到达,我如何捕捉通知数据,或者我只是想播放声音。
    • 我明白这一点,但您还需要一个能够在您的应用处于后台时播放声音的库。您当前的图书馆不会为您做到这一点。因此,您需要解决一系列问题才能获得所需的解决方案。
    • 知道如何处理后台通知吗?使用 react-native-firebase。我正在尝试编写一个将发送和事件发送到本机代码的桥接器。任何替代品都会有很大帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多