react-native-firebase在通知到达时播放自定义声音

时间:2020-01-22 14:56:28

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

我有一个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);
        });

1 个答案:

答案 0 :(得分:1)

您的应用程序中需要一些东西,可以让您在应用程序未专注时进行游戏。我发现react-native-track-player很好,应该提供您所需要的东西。