问题描述
我有一个React本机应用程序,它是一个基于React Hooks的应用程序,我想在其中基于通知有效负载数据播放自定义音乐.而且它仅在打开应用程序时有效,但是我也想在应用程序处于后台时播放自定义声音.
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();
}
})
共享了我的本机代码.还可以共享我从Node.js应用发送的正文
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);
});
推荐答案
您的应用程序中需要一些东西,可以让您在应用程序不专注的情况下进行游戏.我发现 react-native-track-player
很好,应该可以提供您所需的内容.
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.
这篇关于react-native-firebase在通知到达时播放自定义声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!