0

我正在尝试获取有关该地区有多少人的 geoQuery,并向附近的每个其他用户发送有关此更新的通知,我正在使用 expo、expo-notifications 和 geoFire 开发基于位置的查询,我不知道如何这样做,有什么办法吗,如果有,请告诉我.. 谢谢。:) 这是我现在拥有的代码,但问题是,随着人数的增加,会发送过多的通知。

        const location = await Location.getCurrentPositionAsync();
        const userLocation = [location.coords.latitude, location.coords.longitude];
        const firebaseRef = await firebase.database().ref(`geoLocation`);
        const geoFireIndex = await new GeoFire(firebaseRef);
        const geoQuery = await geoFireIndex.query({
            center: userLocation,
            radius: radius,
        });
        await geoQuery.on('key_entered', async function (key, location, distance) {
            try {
                const userRef = await firebase.database().ref('users/' + key);
                await userRef.once('value', async function (snapshot) {
                    const user = snapshot.val();
                    if (key === userId || !user?.expoPushTokens)
                        return console.log('NO NOTIFICATIONS SENT');
                    let expoTokens = Object.values(user?.expoPushTokens);
                    let withOutDuplicates = expoTokens.filter((v, i, a) => a.indexOf(v) === i);
                    withOutDuplicates.map((item, index) => {
                        const message = {
                            to: item,
                            sound: 'default',
                            title: 'Someone is near you !',
                            body: `${SOMEONE} is near you!`,
                            data: {},
                        };
                        fetch('https://exp.host/--/api/v2/push/send', {
                            method: 'POST',
                            headers: {
                                Accept: 'application/json',
                                'Accept-encoding': 'gzip,deflate',
                                'Content-Type': 'application/json',
                            },
                            body: JSON.stringify(message),
                        });
                        console.log('NOTIFICATION SENT', message);
                    });
                });
            } catch (error) {
                Alert.alert('Oops!', 'There seems to be some unexpected error from our side!');
            }
        });
    };

如果您找到任何解决方案,请告诉我,或者如果您对如何解决主要问题有任何其他想法,请告诉我。

4

0 回答 0