我遇到 geofire 问题,我正在使用云功能。 我想如果在radius中输入的键发送通知,但是我的代码发送很多通知。我正在从事android中的项目。
这是我的代码 enter image description here
exports.SendOrder = functions.database.ref("/DealersGeofire/{uids}/{uid}").onWrite((change, context) => {
var dealerLat = 0.0;
var dealerLong = 0.0;
var radiusCC;
var radiusMax = 40;
/*admin.database().ref("/Users").child("Dealers").child(k).once('value',function(g){
name = g.child("name").val()
img = g.child("img").val()
})*/
var k = context.params.uid
var name
var img
radiusCC = change.after.child("radius").val()
dealerLat = change.after.child("l").child("0").val()
dealerLong = change.after.child("l").child("1").val()
console.log("Lat :" + dealerLat + "Long :" + dealerLong, "Radius : " + radiusCC)
var refDriver = admin.database().ref("/DriverActive/DriverGeofire");
if (dealerLat === null || dealerLong === null) return;
var geoFire = new GeoFire(refDriver);
var geoQuery = geoFire.query({
center: [dealerLat, dealerLong],
radius: radiusCC
})
var onKeyEnteredRegistration = geoQuery.on("key_entered", (key, location, distance) => {
console.log("key_entered" + " " + "DriverKey : " + key + " " + "DealerKey :" + k + " " + "distance :" + distance)
return admin.database().ref("Orders").child("OrderPending").child(context.params.uids).child(k).once('value').then((s)=> {
if (s.exists()) {
admin.database().ref("DriverActive").child("Driver").child(key).child(k).once('value').then((g) => {
if (!g.exists()) {
admin.database().ref("Users").child("Rider").child(key).once('value').then((d)=> {
if (d.exists()) {
var Cash = d.child("Cash").val()
var state = d.child("state").val()
var token = d.child("Token").val()
var name = d.child("username").val()
var img = d.child("img").val()
console.log("Error", false)
if (Cash > 0) {
if (!state) {
admin.database().ref("DriverActive").child("Driver").child(key).child(k).set(true, function (a) {
if (!a) {
console.log("Token : " + token)
const payload = {
notification: {
title: "يوجد طلب في متجر",
body: 'يوجد طلب في متجر' + name,
icon: img
}
};
admin.messaging().sendToDevice(token, payload)
}
})
}
}
} else {
console.log("Error", true, a.message.toString())
}
return d.val()
}).catch(()=>{
return null
})
}
return g.val()
}).catch(()=>{
return null
})
}
return s.val()
}).catch(()=>{
return null
})
})
var onKeyExitedRegistration = geoQuery.on("key_exited", (key, location, distance) => {
console.log("key_exited" + " " + "DriverKey : " + key + " " + "DealerKey :" + k + " " + "distance :" + distance)
admin.database().ref("DriverActive").child("Driver").child(key).child(k).remove()
})
var onKeyReadyRegistration = geoQuery.on("ready", () => {
setTimeout(() => {
if (radiusCC !== radiusMax) {
radiusCC++;
console.log("Ready" + " " + " " + "DealerKey :" + k + " Radius: " + radiusCC)
admin.database().ref("Orders").child("OrderPending").child(context.params.uids).child(k).once('value', function (s) {
if (s.exists()) {
admin.database().ref("DealersGeofire").child(context.params.uids).child(k).child("radius").set(radiusCC)
}
return s.val()
})
}
}, 30000)
})
return onKeyEnteredRegistration, onKeyExitedRegistration, onKeyReadyRegistration
})
出什么问题了?我怎样才能做到这一点? 请帮助我