React Native Notifee的通知铃声问题

移动开发 2026-07-11

我在通知上遇到了一个问题。我试图替换通知声音,但没有效果。我甚至更换了通道的ID,但仍然没有用!!

const HomeScreen = ()=>{
    async function displayNotification() {
        //ask the permission to send notifications
        await notifee.requestPermission();
        //create a chennel to send the notification(only for android)
        const channelId = await notifee.createChannel({
            id: 'defaultV0',
            name: 'Default Channel',
            sound: 'my_sound'
        });

        const trigger = {
            type: TriggerType.TIMESTAMP,
            timestamp: Date.now() + 1000,
            repeatFrequency: RepeatFrequency.DAILY
        };

        //Send the notification With a trigger(We can replace it with diplayNotification but it will be sent immeidatly)
        await notifee.createTriggerNotification({
            title: 'Hello',
            body: 'A test',
            android: {
                channelId,
                sound: 'my_sound',
                importance: AndroidImportance.HIGH,
                actions:[
                    {
                        title: 'Just a test',
                        pressAction: {id: 'Just a test'}
                    }
                ]
            },

        },trigger)
    }

    useEffect(()=>{
        //With this we can specify an action (weather the app is open or not), we use OnBackgroundEvent in index.js file
        notifee.onForegroundEvent(({type, detail})=>{
        if(type === EventType.PRESS){
            console.log("The notification has been pressed: " + JSON.stringify(detail.notification));
        }else if(type === EventType.ACTION_PRESS){
            console.log("the action button wes pressed " + detail.pressAction.id);
        }
    });

    }, [])
    return <View>
        <Button title='Sent notification' onPress={()=> {displayNotification()}}/>
    </View>
}

export default HomeScreen;

如果你有任何解决方案,请告诉我。

解决方案

一旦通知渠道创建,其声音就不能再更改。Android会永久缓存渠道设置,除非删除渠道或重新安装应用。

因此,请从设备/模拟器卸载应用并重新安装,或在系统设置中手动删除该渠道。

如果仍然不起作用,请确保在Android中声音文件是小写、没有空格,放在:

android/app/src/main/res/raw/my_sound.mp3

并且代码中也没有扩展名

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章