世博会通知 - 没有声音

问题描述 投票:0回答:1

我正在构建一个使用推送通知的 React Native/Expo 应用程序。为了让它们工作,我使用包 expo-notifications,如下所示:

首先,我要设置通知处理程序

    Notifications.setNotificationHandler({
      handleNotification: async () => ({
        shouldShowAlert: true,
        shouldPlaySound: true,
        shouldSetBadge: false
      })
    })

然后我得到推送令牌:

const registerForPushNotificationsAsync = async () => {
  let pushToken
  if (Constants.isDevice) {
    const { status: existingStatus } = await Notifications.getPermissionsAsync()
    let finalStatus = existingStatus
    if (existingStatus !== 'granted') {
      const { status } = await Notifications.requestPermissionsAsync()
      finalStatus = status
    }
    if (finalStatus !== 'granted') {
      alert(I18n.t('errors.push.token'))
      return
    }
    pushToken = (await Notifications.getExpoPushTokenAsync()).data
    AsyncStorage.getItem('push_token').then((savedToken) => {
      if (pushToken !== savedToken) {
        AsyncStorage.setItem('push_token', pushToken)
        Networking.registerDeviceForPushNotifications(pushToken, Platform.OS)
      }
    })
    console.log(pushToken)
  } else {
    alert(I18n.t('errors.push.physical'))
  }

  if (Platform.OS === 'android') {
    Notifications.setNotificationChannelAsync('default', {
      name: 'default',
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: Colors.primary,
      sound: 'default'
    })
  }

  return pushToken
}

最后我开始听众:

    notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
      setNotification(notification)
    })

    responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
      // navigation here
    })

问题:

发送到我的应用程序的通知正常进行,但无论我做什么,我都无法让声音工作。我希望我的推送消息在到达时播放默认的系统声音,但什么也没有,我也不知道为什么。

javascript react-native push-notification expo
1个回答
1
投票

您的代码片段指的是

Notifications.setNotificationHandler
,仅当应用程序根据文档此处置于前台时才会触发。您必须在应用程序处于前台的物理设备上发送测试通知才能听到声音。

添加自定义声音也可能有所帮助。这是在 app.json 文件中处理的,需要重建,但您可以查看示例并阅读更多内容here

© www.soinside.com 2019 - 2024. All rights reserved.