【问题标题】:How can I add 3 custom sounds in react native iOS?如何在反应原生 iOS 中添加 3 个自定义声音?
【发布时间】:2022-08-22 10:54:03
【问题描述】:

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

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

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

我解决了Android,但我不知道如何在iOS环境中设置。
iOS有像android一样的频道吗?

    标签: ios objective-c react-native


    【解决方案1】:

    我在接收 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_channel_id,
            id: remoteMessage.notification.notificationId,
            title: remoteMessage.notification.title,
            message: remoteMessage.notification.body,
            soundName: remoteMessage.notification.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-push-notification
    @react-native-community/push-notification-ios

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多