世博铃声推送通知

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

我想在博览会中实现类似whatsapp的通知,即使应用程序在后台,通知也可以进入并响起。我正在创建一个食品配送应用程序,因此我希望司机在到达时拨打客户的电话以提醒客户。当应用程序位于前台时,我现在所拥有的功能可以正常工作,但我希望即使应用程序位于后台时它也能工作。下面是我的代码

useEffect(()=> {
        // Add a notification response listener
        const notificationResponseListener = Notifications.addNotificationResponseReceivedListener(
            handleNotificationResponse
        );

        Notifications.setNotificationCategoryAsync('acknowledge', [
            {
                buttonTitle: "Coming",
                identifier: "reply",
                options: { opensAppToForeground: true },
            }
        ]);
        return () =>{ 
            Notifications.removeNotificationSubscription(notificationResponseListener);
        }
    }, [])

    useEffect(() => {
        const loadSound = async () => {
            try {
                await notificationSound.current.loadAsync(require('../assets/audio/alert.wav'));
                await notificationSound.current.setIsLoopingAsync(true);
            } catch (error) {
                console.error('Error loading sound:', error);
            }
        };
        loadSound();
    
        return () => {
            notificationSound.current.unloadAsync();
        };
    }, []);

    // Define a function to handle notification interaction response
    const handleNotificationResponse = async (response) => {
        console.log('response here...')
        await Notifications.dismissNotificationAsync(response.actionIdentifier);
        if (notificationSound.current) {
            await notificationSound.current.stopAsync();
        }
        Vibration.cancel();
    };

    useEffect(() => {
        socket.emit('user_login', user?.id);
    
        socket.on('driver_arrived', async (param) => {
            console.log('driver arrived')
            await Notifications.scheduleNotificationAsync({
                content: {
                    title: param.title,
                    body: param.message,
                    categoryId: 'acknowledge',
                },
                trigger: null
            });
            if (notificationSound.current) {
                await notificationSound.current.playAsync();
            }
            Vibration.vibrate([1000, 2000, 3000], true);
        });

        if (stores.length !== 0) {
            socket.on('store_online', (store_id) => {
                const index = stores.findIndex(store => store.id === store_id);
                if (index !== -1) {
                    const updatedStores = [...stores];
                    updatedStores[index] = { ...updatedStores[index], status: true };
                    setStores(updatedStores);
                }
            });
    
            socket.on('store_offline', (store_id) => {
                const index = stores.findIndex(store => store.id === store_id);
                if (index !== -1) {
                    const updatedStores = [...stores];
                    updatedStores[index] = { ...updatedStores[index], status: false };
                    setStores(updatedStores);
                }
            });
        }
    
        return () => {
            notificationSound.current.pauseAsync();
            socket.off();
        };
    }, [stores, user]);
    
reactjs node.js react-native sockets push-notification
1个回答
0
投票

我正在开发一个像你这样的应用程序, 您可以联系我吗?

[电子邮件受保护]

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