我有一个Android项目,在不同的设备上有2个应用程序。我使用Firebase设置了后端服务。我有两种不同类型的用户,Rider
和Driver
。从RiderApp,我保存到数据库。
我想做的是检索保存到数据库中的值。
Firebase被设置为一个项目,两个应用共享相同的google-services.json。
我的问题是,如果我在DriverApp中,如何获取theRiderApp的用户名?
如果我将userId = firebaseUser.getUid()->获取我所在的应用程序的当前驱动程序,但如何获取RiderApp的userId才能成功完成此“读取Firebase”
Firebase结构示例:
{
"Requests": {
"khVbT88fA8YUmt5qv5LG0OTVZEm1": {
".priority": "f81189yp46",
"destination": {
"searchBar": "43 Union Street"
},
"g": "f81189yp46",
"l": [
45.274498,
-66.0634308
]
}
},
"Users": {
"Drivers": {
"RAFIxzcAYjNNIU15IMIVHfr0CMC2": {
"email": "driver2@me.com",
"name": "LizG",
"password": "zzzzzz",
"phone": "6532525"
},
"vP9r4F2yDWRRuvKjRiQvMEXVuoK2": {
"email": "driver@me.com",
"name": "Batman",
"password": "zzzzzz",
"phone": "5326598",
"rates": "0"
}
},
"Riders": {
"khVbT88fA8YUmt5qv5LG0OTVZEm1": {
"avatarUrl": "",
"email": "rider@me.com",
"name": "Rider",
"password": "zzzzzz",
"phone": "5551211",
"rates": "0"
}
}
}
}
UserId =“用户/乘员”当前的userId 我正在从我的DriverApp中检索“ Requests / userId / destination / searchBar
中的值任何帮助将不胜感激。
已编辑
private void findDriver() {
final DatabaseReference drivers = FirebaseDatabase.getInstance().getReference(
Common.driver_tbl);
GeoFire gfDrivers = new GeoFire(drivers);
GeoQuery geoQuery = gfDrivers.queryAtLocation(new GeoLocation(Common.mLastLocation.getLatitude(),
Common.mLastLocation.getLongitude()), radius);
geoQuery.removeAllListeners();
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
// if found
if (!Common.isDriverFound) {
Common.isDriverFound = true;
Common.driverId = key;
btnRequest.setText("CALL DRIVER");
// Toast.makeText(RiderHome.this, "Request Sent",
// Toast.LENGTH_LONG).show();
}
}
@Override
public void onKeyExited(String key) { }
@Override
public void onKeyMoved(String key, GeoLocation location) { }
@Override
public void onGeoQueryReady() {
// if still not found driver, increase distance
if (!Common.isDriverFound && radius < LIMIT) {
radius++;
findDriver();
} else if (!Common.isDriverFound) {
Toast.makeText(RiderHome.this, "There are NO available drivers near you.",
Toast.LENGTH_LONG).show();
btnRequest.setText("REQUEST RYYDE");
}
}
@Override
public void onGeoQueryError(DatabaseError error) {}
});
}
更新代码
当运行产生结果时: {RBUUID8JNihtbMmvwgcjZVgj2Ku2 = {driver = wLY5SqTxiLTUhToZdcyRh8uqNKF3,g = f8118dn88t,destination = {tapOnMap = Somerset St},l = [45.2747733,-66.0628253]}}
Query query = FirebaseDatabase.getInstance().getReference().child("Requests")
.orderByChild("driver").equalTo(driverId);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String riderDest = String.valueOf(dataSnapshot.getValue());
etDestination.setText(riderDest);
Log.d(TAG, "riderDest = " + riderDest);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
答案 0 :(得分:1)
如何获取theRiderApp的用户名?
除非在骑手和驾驶员之间建立连接,否则您不能。因此,在创建请求时,还应将相应的驱动程序分配给特定的request
。然后,您只需查询数据库即可从请求对象中获取所有数据,包括相应的驱动程序。
您更改后的请求节点应如下所示:
"Requests": {
"khVbT88fA8YUmt5qv5LG0OTVZEm1": {
".priority": "f81189yp46",
"destination": {
"searchBar": "43 Union Street"
},
"g": "f81189yp46",
"driver": "RAFIxzcAYjNNIU15IMIVHfr0CMC2", //driver id added
"l": [
45.274498,
-66.0634308
]
}
},
如您所见,现在请求对象包含驾驶员ID和属性ID作为驾驶员节点的键。