When uploading the post, I'm trying to get the postKey and upload it to dynamiclink, but I'm getting wrong postKey (first 6 characters in the key are right then the rest are wrong). How can I get the right postKey when uploading the post?

This is my post including the right postKey:

post image

And this is what I'm getting -LYelE9TdCU73qW1Xisc , which also is not a key for any other posts.

My code

String postKey = FirebaseDatabase.getInstance().getReference().child("posts").push().getKey();
        Log.i("postkey for dynamiclink",postKey);

Very much appreciated your help!

Answer
      • 2
    • Thanks Peter, you are right I was creating another postkey. I removed the duplicated .push() and using my same line of code it solved the problem.

In your code, you are creating another postKey by using push().getKey().

To retrieve the key, that is already in the database, try the following:

  DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");

reference.orderByChild("title").equalTo("test").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
  for(DataSnapshot datas: dataSnapshot.getChildren()){
     String keys=datas.getKey();
    }
 }
@Override
public void onCancelled(DatabaseError databaseError) {
    }
 });
  • 1
Reply Report
    • Thanks Peter, you are right I was creating another postkey. I removed the duplicated .push() and using my same line of code it solved the problem.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions