Solution 1 :
Assuming that the MyPage
is the root of your database, please use the following lines of code:
val rootRef = FirebaseDatabase.getInstance().reference
val messageRef = rootRef.child("message")
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.children) {
val id = ds.child("id").getValue(String::class.java)
val text = ds.child("text").getValue(String::class.java)
val time = ds.child("time").getValue(String::class.java)
Log.d("TAG", text + " " + id + " " + time)
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("TAG", databaseError.getMessage()) //Don't ignore errors!
}
}
messageRef.addListenerForSingleValueEvent(valueEventListener)
And the result in your logcat will be:
hi user 2020.06.07/08:22:46
Solution 2 :
Map data = remoteMessage.getData();
String id= data.get(“id”);
You can parse the body from your RemoteMessage.
Problem :
My Firebase data structure is
MyPage
message{
-M9CrzvBuz1EBojnK9xP{
id: user
text: hi
time: "2020.06.07/08:22:46"
But I don’t know how to get id, text and time. How can I get it with Kotlin?
The number of children in the message will continue to increase. Can I get them all?