Android chat app Checking online/offline users firebase - java

I am trying to add online/offline feature in my android chat app using .info/connected path
I wrote the following code inside onCreate() method
studentref = FirebaseDatabase.getInstance().getReference("student").child(user.getUid());
connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
studentref.child("status").onDisconnect().setValue("offline");
studentref.child("status").setValue("Online");
} else {
studentref.child("status").setValue("offline");
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
But the else part does not execute when i minimized the app for more than 60 seconds
It only works when i killed the app or when i switch off the internet for more than 60 seconds
How to make it works , when the app is in foreground it should set the value "online" and when the app is in background or killed it should set the value to "offline"

I solved this issue using 2 ways
First we need to detect when the app goes to the background and come back to the foreground , so when the app goes to foreground update the user state as "Online"
when the app goes to background update the user state as "Offline"
We can achieve this by implementing LifecycleObserver class and writing the following 2 methods
#OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onAppBackgrounded() {
//App in background
// here update user state as offline
}
#OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onAppForegrounded() {
// App in foreground
// here update user state as online
}
But this will not work when there is no internet connection , Eg: when the user switch off the internet while the app in foreground and close the app in this case the user state will remain online even the app is closed , To solve this we need also to check the connection to .info/connected path
connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
listenerCon = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
user= FirebaseDatabase.getInstance().getReference("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
user.child("state").setValue("Online");
user.child("state").onDisconnect().setValue(ServerValue.TIMESTAMP);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
};
connectedRef.addValueEventListener(listenerCon);
So when there is no connection for more than 60 sc the user state will be updated to offline

The .info/connected reflects the connection of your application to the backend servers of the Firebase Realtime Database. While this may have some relation to whether the user is actively using the app, it is not a 1:1 mapping.
If you want to detect whether the app is backgrounded, there are better ways (unrelated to Firebase) such as How to detect when an Android app goes to the background and come back to the foreground and others from these search results.

Use my code, in onCreate
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String id = user.getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child(id).child("stateOnline");
if (user != null){
reference.setValue(true);
reference.onDisconnect().setValue(false);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
// CONNECTED
} else {
// NOT CONNECTED
}
}
#Override
public void onCancelled(DatabaseError error) {
System.err.println("Listener was cancelled");
}
});
reference.keepSynced(true);
}

Related

Updating a List Size for all users

I am trying to make it so that when a user creates an open game, in my Firebase Database the name of the game is the size of the list. So the very first game created the name of it will = 0, and if another user creates a game then the game will be labeled 1 and so on.
I have it set up right now so that the games are labeled the size of the game list, but the list isn't really updating. The games keep getting called '0' because it thinks the list is empty even though I have visual confirmation in the app that there are items being added to the list.
So my question is: How can I make it so the list continuously updates each time a game is added, and how can I make it so that it updates for all users and not just the user who created the game?
This is what I have setup right now. Here are the variables I am using for the list and the integer getting the list size
ArrayList<String> openGames = new ArrayList<>();
int gameSlot = openGames.size();
Here is what I use to name the game when it is created.
gameMaker = new GameMaker(hp.uid, userName, wagerD, gameSlot);
FirebaseDatabase.getInstance().getReference("FCGames").child(Integer.toString(gameSlot))
.setValue(gameMaker).addOnCompleteListener...
And this is what I have to add the game to the list.
cgRef.child(Integer.toString(gameSlot)).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
openGames.add(userName);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
So again my question is how can I make this list update correctly and how can I make it update for all users on the app?
Edit
Here is what I did with my onChangeData
cgRef.child(Integer.toString(gameSlot)).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
wager = (String) dataSnapshot.child("wager").getValue();
gameSlot = openGames.size();
adapter.notifyDataSetChanged();
}
and now the openGames.add is in my createGameLobby method.
FirebaseDatabase.getInstance().getReference("FCGames").child(Integer.toString(gameSlot))
.setValue(gameMaker).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
openGames.add(userName);
Toast.makeText(FlipCoinLobby.this, "Game creation successful.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(FlipCoinLobby.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
^^ that is just the important snippit from the method. And then I have an onClickListener that creates that calls that method when a button is pressed
In below code segment, you did that openGames.add(username) in onDataChange listener. I think it is an incorrect use of this ondatachange function
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
openGames.add(userName);
adapter.notifyDataSetChanged();
}
please use this to get data from db and update your data. Then push your data to db. You didn't use snapshot value also. You can get most recently updated data from dataSnapshot . Use it to update user data then push it to db

Android firebase no data query

Im using the Android SDK for firebase database.
In the database I have a structure where I keep the messages ordered by user, that way if I need their messages I just query by user.
myDatabase -> messages -> JonDoe
-> "You forgot your mail"
-> "Buy groceries"
-> JaneDoe
-> "Dog's birthday!"
The problem is if the user doesnt exist the listener keeps waiting forever, and I want to show "You have no messages" in that case. (For example, if I query the user "CharlesDoe" in the example above)
Is there a way to check if a reference exists before/after/during a query?
try this:
DatabaseReference root =FirebaseDatabase.getInstance().getReference();
DatabaseReference user = root.child("myDatabase").child("messages");
user.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.child("CharlesDoe").exists()) {
// run some code
}else{
Toast.makeText(this,"no messages",Toast.Length_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here it will query on node messages and then it will check if the child exists and do the required.
Try this on
DatabaseReference root =FirebaseDatabase.getInstance().getReference();
DatabaseReference user =
root.child("myDatabase").child("messages").child("CharlesDoe");
user.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
// run some code
}else{
Toast.makeText(this,"no messages",Toast.Length_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

How can I check if my Firebase database has data for a certain user? (Android/Java)

When a user makes an account with firebase on my android app, they then need to be directed to a screen where they can add some separate information like username, for example. The app knows to switch the activity through an AuthStateListener, so when an account is made or someone signs in the code here is executed (no intents are changed here right now, keep reading to see why):
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
userID = user.getUid();
toastMessage("User has signed in: " + user.getEmail());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
In the if block where it is checking to see if user does not equal to null, this is where I need to change the intent to another activity where they will enter their username. Basically the logic is, if the user already has a username and they are just signing in then they will be directed to the home screen activity, and if they are signing up they need to be directed to the activity where they make a username. To know which activity I need to send them to I need to have the code look in the firebase database and see if their account has a username already or not. I cannot figure out how to read data from the firebase in this situation, it seems as if the only way to read from the database (according to others) is through an onDataChange methods like so:
mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "This: "+dataSnapshot.getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
which uses dataSnapshots. the problem with this is this code is only fired when data is added to the database, therefor this method will not execute any code when a user is simply signing up and has never added a username to the database. How can I query to see data from the database just to check if a certain user has any data there?
OnDatachange is fired in all of cases, if user add data, remove data, change data ect.
To check is user already have username do this inside OnDatachange:
if(dataSnapshot.exists()){
//GO TO OTHER ACTIVITY
]else{
//GO TO SET USER DATA ACTIVITY
]
See this exemple of one of my implementations:
if (user !=null){
DatabaseReference myRef = database.getReference("users").child(user.getUid()).child("user_bio");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
bio.setText(dataSnapshot.getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
In this exemple if user_bio exists then set text to userbio if not do nothing. In the first time that user log in in my app this method do nothing but when user add bio this method set the bio text in userbio text. but if i want to show some text if user hasnt bio i simply add na else and set text to the bio for exemple:
if (user !=null){
DatabaseReference myRef = database.getReference("users").child(user.getUid()).child("user_bio");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
bio.setText(dataSnapshot.getValue(String.class));
}else{
bio.setText("no user bio");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Have you try to use addValueEventListener inside onCreate method?
If you run an activity while the listener inside onCreate method it will fired once when you open an activity. So i think you shouldn't have a problem here.

Call firebase getvalue oncreate

mRefQ.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String txt = dataSnapshot.getValue(String.class);
showtext.setText(txt);
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
as it stands i can only access the value when the data is changed but i want to access it whenever i want(namely oncreate).
From the Firebase documentation for listening to value events:
This method is triggered once when the listener is attached and again every time the data, including children, changes.
You can access the Data only once by following this steps.
As #Frank mentioned having a listener will give you access when attached and every time it changes.
Bear in mind that all of this happens asynchronously on a separate thread, therefore in order to ensure the value is available on onCreate you may have to do the orchestration yourself and hold the main thread until the values is ready to be used.
Alright so it turns out it does call it when created, i just had to wait for the firebase to load up on the app
It is not triggerred once in onCreate() method. Without removeEventListener, addValueEventListener will never stop if you listen it in OnCreate() Method. Count is increasing and never stops and load was up to 85% when I run it and so I had to close my internet and uninstall it. But ienter image description heret has just been solved. Here's my code if you wanna check it.
Variables are declared global...
private DatabaseReference main;
private ValueEventListener valueEventListener;
In onCreate() Method.....
valueEventListener = main.addValueEventListener(new
ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean countExists = false;
int count = 1; //starts from 1 when data is uploaded for the first time
for (DataSnapshot out: dataSnapshot.getChildren()) {
if (out.getKey().equals("count")) {
String temp = out.getValue(String.class);
countExists = true;
try {
count = Integer.parseInt(temp);
}
catch (NumberFormatException e) {
count = 1;
}
break;
}
}
if (!countExists) {
main.child("count").setValue(String.valueOf(count));
Toast.makeText(getApplicationContext(), "Count Created", Toast.LENGTH_SHORT).show();
upload.setClickable(true);
}
else {
main.child("count").setValue(String.valueOf(++count));
Toast.makeText(getApplicationContext(), "Count Updated", Toast.LENGTH_SHORT).show();
upload.setClickable(true);
}
main.removeEventListener(valueEventListener);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "Failed to upload", Toast.LENGTH_SHORT).show();
}
});
Make sure you add removeEventListener when you call it in onCreate() method or it will never stops. It is not a problem if you don't call removeEventListener in onStart() method.
This is not your question but...
If you wanna use firebase for likely free, you can't let all users to download your data without necessary or your bandwidth may become full within a day. So, I count the data and just download that child's value and compare the result and make update. So it can reduce your bandwidth when you call addValueListener.

How does Firebase sync work, with shared data?

I use Firebase to handle the Auth topic of my Android app.
I also save a user profile on Firebase, that contain user id and extra options that user can update in the android app.
At startup, the app check the auth, and auth. It reload the user profile (on Firebase) to then update my userInstance on the app.
Firebase is set as offline capable at app level.
The userInstance POJO is sync with Firebase at log and stop to be sync at unlog.
I have a special parameter, in my user profile that I can change (as an admin).
Every-time I update it on the Firebase console, it is replaced by the previous value when the app start again.
How can this happen ?
BTW :
1/ Based on which mechanism are the data merged, if multiple client have different local values ?
Here is simpler code, where I tried to reproduce the error. :
MyApplication.java
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
Firebase.getDefaultConfig().setLogLevel(Logger.Level.DEBUG);
Firebase.getDefaultConfig().setPersistenceEnabled(true);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
Firebase ref;
User user;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ref = new Firebase("https://millezim-test.firebaseIO.com").child("user");
ref.keepSynced(true);
Button br = (Button) findViewById(R.id.b_read);
Button bs = (Button) findViewById(R.id.b_save);
final TextView tv_r = (TextView) findViewById(R.id.tv_value_toread);
final EditText tv_s = (EditText) findViewById(R.id.tv_value_tosave);
user = new User();
bs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!tv_s.getText().toString().equalsIgnoreCase(""))
user.setAge(Integer.valueOf(tv_s.getText().toString()));
ref.setValue(user);
}
});
br.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User u = dataSnapshot.getValue(User.class);
if (u != null)
tv_r.setText(String.valueOf(u.getAge()));
else
tv_r.setText("Bad Value");
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
});
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User u = dataSnapshot.getValue(User.class);
u.setCounter(u.getCounter() + 1);
user = u;
saveUser();
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
public void saveUser() {
ref.setValue(user);
}
}
If you just change a value in the app, then the counter inc until the app stop : this is normal. But what is strand is the age pass from old to new then to old cyclically without stopping.
And I feel that behavior in my app, without the cyclic, as I do not have a counter, but I cannot change a parameter in the admin client, I always get back the previous value stored in the mobile.
I just Auth, then I update my UserInstance with AuthData + the User fetch from Firebase (probably the cached data), and then I save back the updated User under Firebase (As I may got new AuthData, and I normally get the latest User from Firebase)
2/ In this simple example, I saw that if I read the value at start, it fetch the data cached in the app. How can I force having online data ?
The problem comes from the fact that you're using disk persistence with a single-value event listener. When you do:
ref.addListenerForSingleValueEvent(new ValueEventListener() {...
You're asking for a single value of that location (the user in your case). If Firebase has a value for that location in its local cache, it will fire for that value straight away.
The best way to solve this is to not use a single-value listener, but instead use a regular event listener. That way you will get two events: one for the cached version and one for the version that comes back from the server (if it is different).
The only alternative is to not use Firebase's disk persistence. Without that, there won't be a local cache for the data to be read from upon a restart.
There were a few discussions about this combination on the Firebase mailing list. Here's one: https://groups.google.com/forum/#!msg/firebase-talk/ptTtEyBDKls/XbNKD_K8CQAJ
After digesting a bit, my current strategy is to use Firebase for my data persistance, and not use anymore my own objects. (Before I had to sync UI, my data, the firebase cache, and the server data)
So now, I use
use disk caching
use onValueEventListener
keep update data (only to be read with component that need sync data)
trigger event that update UI (for component that can accept async data)
define some specific setter, that update data on Firebase (not anymore at app level)
It does means, that when I update a data, it goes to the server (or Firebase caching layer) until it goes back to the UI. As firebase handle this caching, if fast this is fast enough, and this is Firebase that deal with network sync.
To bring the (1) solution from #frank-van-puffelen into code :
public class MainActivity extends AppCompatActivity {
Firebase ref;
User user;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ref = new Firebase("https://test.firebaseIO.com").child("user");
ref.keepSynced(true);
Button br = (Button) findViewById(R.id.b_read);
Button bs = (Button) findViewById(R.id.b_save);
final TextView tv_r = (TextView) findViewById(R.id.tv_value_toread);
final EditText tv_s = (EditText) findViewById(R.id.tv_value_tosave);
user = new User();
bs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!tv_s.getText().toString().equalsIgnoreCase(""))
user.setAge(Integer.valueOf(tv_s.getText().toString()));
ref.setValue(user);
}
});
br.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User u = dataSnapshot.getValue(User.class);
if (u != null)
tv_r.setText(String.valueOf(u.getAge()));
else
tv_r.setText("Bad Value");
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
});
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User u = dataSnapshot.getValue(User.class);
u.setCounter(u.getCounter() + 1);
user = u;
saveUser();
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
public void saveUser() {
ref.setValue(user);
}
}
However, this change nothing, even worst. Now it seems that every value set, stay as a ghost value (hold by client/server request), and the value toggling can be seen with every values set !
EDIT
The following code worked out !
Having a normal ValueListener, that you stopped before saving again a value, and you enable back when save is completed ! (Ok I was thinking this may be done in the Firebase Framework).
public class MainActivity extends AppCompatActivity {
Firebase ref;
User user;
private ValueEventListener theListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ref = new Firebase("https://test.firebaseIO.com").child("user");
ref.keepSynced(false);
Button bs = (Button) findViewById(R.id.b_save);
final EditText tv_s = (EditText) findViewById(R.id.tv_value_tosave);
user = new User();
bs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!tv_s.getText().toString().equalsIgnoreCase(""))
user.setAge(Integer.valueOf(tv_s.getText().toString()));
ref.setValue(user);
}
});
theListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User u = dataSnapshot.getValue(User.class);
u.setCounter(u.getCounter() + 1);
user = u;
updateUI(u);
saveUser();
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
};
ref.addValueEventListener(theListener);
}
public void saveUser() {
ref.removeEventListener(theListener);
ref.setValue(user, new Firebase.CompletionListener() {
#Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
ref.addValueEventListener(theListener);
}
});
}
public void updateUI(User user) {
TextView tv_r = (TextView) findViewById(R.id.tv_value_toread);
if (user != null)
tv_r.setText(String.valueOf(user.getAge()));
else
tv_r.setText("Bad Value");
}
}
EDIT
However this do not allow to change a value on the admin page. The age value is set and then remain back to the one that is save on the mobile.
So I imaging then the only solution is to solve at system level. DO NOT USE VALUELISTENER FOR VALUE THAT AN APP CAN SAVE AND THAT CAN BE SAVED BY THIRD PARTY APP. Please advise/correct this assumption !

Categories

Resources