I'm making a Android chatting app with firebase.
When Framgment is creating a view, I try to get user lisst,
But it has delay like 4~5 sec. Is there faster way to get data immediately with firebase?
Firebase firebase = new Firebase(AppDefine.FIREBASE_URL).child("friendlist-"+ac.getUserId());
firebase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
HashMap<String,String> map = (HashMap) dataSnapshot.getValue();
mFriends.add(new ChatUserInfo(map.get("id"),map.get("name")));
mAdapter.notifyDataSetChanged();
}
}
Related
I'm developing a social networking app, where users can follow other users and like their posts, comments ...
Whenever some user follows someone, it shows in a notification fragment in the other user account to inform him that he has a new follower.
The problem is that I couldn't remove the notification when the user hits unfollow. Here is what I have tried:
if ( holder.btn_follow.getText().toString().equals("follow"))
{
addNotifications(user.getId());
}
else{
FirebaseDatabase.getInstance().getReference("Notifications").child(user.getId()).removeValue();
}
and here is how I added notification:
private void addNotifications(String userid)
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(userid);
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("userid", firebaseUser.getUid());
hashMap.put("text"," started following you");
hashMap.put("postid","");
hashMap.put("ispost",false);
reference.push().setValue(hashMap);
}
the problem with my code is that whenever the user unfollows someone, all the notifications from that user are deleted including his likes and comments. all I want is to delete "started following you".
Here is how it looks in the Firebase database.
Use Firebase Query refers to Firebase Docs, it will be something like this
Query queryRef = mReference.child("Notifications").child(userId).orderByChild("text").equalTo("start following you");
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot snapshot, String previousChild) {
// snapshot.getRef().setValue(null);
snapshot.getRef().remove();
}
});
I have to load approx 5000 user data from firebase realtime database and show it to user in list so i need to implement loadmore functionality below is the code which load all the data
mDatabase.child(getString(R.string.users)).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();
usersList.clear();
if(newPost!=null){
// perform data manupulation
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
loading.dismiss();
}
});
I have tried limittofirst and limittolast but its not sort out my problem as i need data in-betweeen after first load and also like to optimize dowload as well so that data wont get downloaded again and again i.e first 100 then first 200 and so on its dowloaded the same data again and consume the firebase bandwidth
Can you please tell me how to implement loadmore functionality in firebase realtime database in effective way?
I have recently added a chat feature to my app.
I am storing the messages and their timestamps in Firebase database.
I am trying to figure out a way of displaying (in the current chat room) only the last (let's say) 60 messages. This would mean retrieving only the last 60 items of the current conversation from Firebase using limitToLast().
Furthermore I would like to add a Load more button that would appear only when the 60 messages limit has been reached by swiping up and it should load another 60.
Is there a proper way of handling the message archive as I stated above?
Using the code below I actually retrieve the whole specific message archive. I find this ineffective when the specific chat archive has thousands of messages.
mChildEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ChatMessage chatMessage = dataSnapshot.getValue(ChatMessage.class);
mMessageAdapter.add(chatMessage);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
mDatabaseReference.child("chat_messages").child(chatId).addChildEventListener(mChildEventListener);
To achieve this, I recomand you using the following logic:
pageEndOffset = 0;
pageLimit = 60;
pageEndOffset += pageLimit;
Query query = mDatabaseReference.child("chat_messages")
.orderByChild("chatId").limitToFirst(pageLimit)
.startAt(pageEndOffset);
query.addValueEventListener(YourActivity.this);
Hope it helps.
I am building a social network like Instagram. I have worked on many social networks before, using mySQL. I am new to firebase server.
I want to search users via name and want to show the follow/following button on the list view. But I am little confused to run android firebase queries in a standard format. I do not want to run unnecessary loops. Below is my database structure for the follow table.
Node name is follow and follower_id refers to the user who is following the user and user who gets followed is referred as followed_id.
How to write a simple query using android firebase to show all the users with the name starting (e.g "an") and with the status that I am already following him/her or not.
FYI: I am not using firebase rest API's.
How to write a simple query using android firebase to show all the users with the name starting (e.g "an") and with the status that I am already following him/her or not.
I think you can't do it in a single query.
You could try to do it with nested queries, something like this:
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// Retrieve the userId
User user = dataSnapshot.getValue(User.class);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot usersFollowedDataSnapshot) {
for ( DataSnapshot userFollowedDataSnapshot : usersFollowedDataSnapshot.getChildren() ) {
// Retrieve the follower structure
Follow follow = userFollowedDataSnapshot.getValue(Follow.class);
if ( myUserId == follow.getFollowerId() ) {
// This is a user that I'm following and which name starts with "am"
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
Query secondQuery = mFirebaseDatabaseReference.child("follow").orderByChild("followedId").equalTo(user.getId());
secondQuery.addListenerForSingleValueEvent(valueEventListener);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
Query firstQuery = mFirebaseDatabaseReference.child("users").orderByChild("name").startAt("am").endAt("am\uf8ff");
firstQuery.addChildEventListener(childEventListener);
I know it's not very straightforward and looks disapponting. It could be also slow, but it worth trying.
Alternatively, you should consider to structure your database in a way to simplify the firebase queries.
See more on firebase queries for example here and here.
I'm making chat application with Firebase Realtime Database.
I Made
public class MessageAdapter extends BaseAdapter implements ChildEventListener{
...
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message msg = dataSnapshot.getValue(Message.class);
this.addItem(msg);
this.notifyDataSetChanged();
}
...
}
and this MessageAdapter refresh my Listview when data is added to Firebase.
But
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message msg = dataSnapshot.getValue(Message.class);
this.addItem(msg);
this.notifyDataSetChanged();
}
onChildAdded Method is called whenever I started app. I want to load Message Only after User starting app. It loads every messages which is stored in my Firebase Realtime Database.
Is there a way to load only new message after user starting application? and furthermore I want to make this to load specific number of messages like if I set the number as 5, when user start an app it loads from Firebase Database only 5 stored messages.
Okay, so there are a couple questions here, first would be to make the app wait before querying the data base. To do this I would suggest pausing the thread for a few seconds while it loads the data using a handler.
handler1.postDelayed(new Runnable() {
#Override
public void run() {
// Your Database Reference Listener
}
}, 1000);
also to listen to only a few from firebase just change the listener like so
yourDatabaseReferance.limitToFirst(10).addChildEventListener(new ChildEventListener()