简体   繁体   中英

react-native-firebase play custom sound on notification arrived

I have a react native app, Its a react hooks based app, where i want to play custom music based on notification payload data. And its working only when app is opened, But i want to play the custom sound when app is in background too.

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();
    }
  })

shared my react native code. Also sharing the body which i am sending from my Node.js app

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

You need something in your application that will allow you to play whilst the app is not focused. I've found react-native-track-player to be quite good and should provide what you need.

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