繁体   English   中英

如何在反应原生 iOS 中添加 3 个自定义声音?

[英]How can I add 3 custom sounds in react native iOS?

我想在反应原生 iOS 中添加 3 个自定义声音。
你们有没有人解决过它?

目前,当我通过将语音文件 (.wav) 添加和分组到 iOS 项目文件夹来测试 FCM 通知时,其中一个添加的声音会出现。

例如,假设你有声音文件sound01、sound02、sound03,当后端发送FCM通知时,我想让指定的声音在那个时候发出声音。

我解决了 Android 但我不知道如何在 iOS 环境中设置。
iOS 是否有类似 android 的通道?

我在接收 Firebase Cloud Messaging (FCM) 的源代码中使用以下代码解决了这个问题。


// remote message processing function
const sendLocalNotificationWithSound = remoteMessage => {

   if (Platform.OS === 'ios') {
      PushNotificationIOS.addNotificationRequest({
        id: remoteMessage.notification.notificationId
          ? remoteMessage.notification.notificationId
          : new Date().toString(),
        title: remoteMessage.notification.title,
        subtitle: remoteMessage.notification.message 
          ? remoteMessage.notification.message 
          : '',
        body: remoteMessage.notification.body,
        sound: remoteMessage.notification.sound
      })
    } else {
      PushNotification.localNotification({
        channelId: remoteMessage.notification.android.channelId,
        id: remoteMessage.notification.notificationId
          ? remoteMessage.notification.notificationId
          : new Date().toString(),
        title: remoteMessage.notification.title,
        message: remoteMessage.notification.body,
        soundName: remoteMessage.notification.android.sound,
        playSound: true,
        smallIcon: 'ic_stat_ic_notification',
        color: '#FFFFFF',
        largeIcon: '',
        largeIconUrl: '',
        vibrate: true,
        groupSummary: true
      })
    }

  }


// remote message receiving
React.useEffect(() => {
  const getMessage = messaging().onMessage(remoteMessage => {
    sendLocalNotificationWithSound(remoteMessage)      
  })

  return () => getMessage()
}, [])

首先,必须安装react-native-push-notification@react-native-community/push-notification-ios库。

反应原生推送通知
@react-native-community/push-notification-ios

代码中的 function messing messaging()@react-native-firebase/messaging库。 你必须导入模块,如:

import messaging from '@react-native-firebase/messaging'

代码中的头部

react-native FCM(firebase 云消息传递)的官方文档在这里 -> REACT NATIVE FIREBASE


FCM发送端JSON样本文件
(我用邮递员测试)

{
    "to": "your fcm token here",
    "notification": {
        "title": "your notification title",
        "body": "your notification description",
        "sound": "your notification sound name (Runs on iOS)",
        "android_channel_id": "your android channel id here (Runs on Android)",
        "priority": "high",
        "contentAvailable": true,
        "mutableContent": 1
    },
    "data": {
       ... // if you need data property
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM