You can simply use a function to check if you have network connection, by pinging Google servers:
/system/bin/ping -c 1 8.8.8.8
In Android, this function looks like this:
public boolean isNetworkAvailable() {
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = process.waitFor();
return (exitValue == 0);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return false;
}
In Firestore, offline persistence is enabled by default. So you can check if the user reads data from the cache or from Firebase servers. A more elegant way would be to use isFromCache()
function. This is the code for Android:
yourDocRef.addSnapshotListener(new DocumentListenOptions().includeMetadataChanges(), new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
Log.d("listener.isFromCache: " + documentSnapshot.getMetadata().isFromCache());
}
});