简体   繁体   English

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

[英]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.分享了我的 React 本机代码。 Also sharing the body which i am sending from my Node.js app还共享我从我的 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);
        });

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-track-player非常好并且应该提供你需要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 React-Native-Firebase 接收通过 iOS 中的 Firebase 控制台发送的推送通知 - Unable to receive push notification send via Firebase console in iOS using React-Native-Firebase firebase web 或 react-native-firebase - firebase web or react-native-firebase 打开应用程序后如何删除 react-native-firebase/messaging 中的通知标记计数 - How to remove the notification badge count in react-native-firebase/messaging after opening the app 混淆将 react-native-firebase 与 Expo 集成 - Confusion integrating react-native-firebase with Expo 在 react-native-firebase 中找不到 this.getOptions - this.getOptions not found in react-native-firebase Firebase/CoreOnly 的@react-native-firebase pod 版本冲突 - @react-native-firebase pod version conflict for Firebase/CoreOnly 如何使用 react-native-firebase/messaging(FCM) 从栏点击通知后将应用程序打开到特定页面? - How to open app to specific page after click notification from bar by using react-native-firebase/messaging(FCM)? 任务 ':react-native-firebase:compileDebugJavaWithJavac' 执行失败。 #2099、 - Execution failed for task ':react-native-firebase:compileDebugJavaWithJavac'. #2099 , React-native-firebase 应用内消息不工作 - React-native-firebase in-app-messaging not working 使用 react-native-firebase 的 Firestore 请求失败 - Firestore request, using react-native-firebase, is failing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM