我正在构建一个使用推送通知的 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
})
问题:
发送到我的应用程序的通知正常进行,但无论我做什么,我都无法让声音工作。我希望我的推送消息在到达时播放默认的系统声音,但什么也没有,我也不知道为什么。