How to verify if user has network access and show a pop-up alert when there isn't

1,220

Solution 1

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());
    }
});

Solution 2

You can use the connectivity package https://pub.dartlang.org/packages/connectivity Here is a simple tutorial with the dart file attached https://medium.com/@aseemwangoo/internet-connectivity-in-flutter-a6b6aedf2964

Possible duplicate - Check whether there is an Internet connection available on Flutter app

Share:
1,220
Pablo de la Fuente
Author by

Pablo de la Fuente

Student. Learning to code with Flutter and Firebase!

Updated on December 06, 2022

Comments

  • Pablo de la Fuente
    Pablo de la Fuente over 1 year

    My name is Pablo and I am currently building a Flutter app. So, my app gets some images and audios from Firebase Storage, and obviously, without the internet connection, the app doesn't display the images and doesn't play the audios. I want the app to pop-up an alert to the user when there is no WIFI nor Data; how can I do that?

    Thanks :)

  • Pablo de la Fuente
    Pablo de la Fuente over 5 years
    Thank you so much!
  • Pablo de la Fuente
    Pablo de la Fuente over 5 years
    Thanks for your answer!!
  • VanessaF
    VanessaF over 2 years
    @Alex Mamo: I just copied and pasted your first approach with isNetworkAvailable() and I always get a false, altough I have Internet connection. Any idea why this is happening?
  • Alex Mamo
    Alex Mamo over 2 years
    @VanessaF Most likely because you aren't reaching Goole servers. If you ping from the command prompt, are you getting the same behavior?
  • VanessaF
    VanessaF over 2 years
    I used "ping 8.8.8.8" in windows 5 times. 2 times 4/4 packages were sent. 2 times 1/4. And one time 3/4. But with your suggested method in Java I always get a false (I tried it more than 30 times)
  • VanessaF
    VanessaF over 2 years
    I tried another 50 times with your suggested method and meanwhile use the windows ping. While in the windwons ping I always get at least 1 out of 4 packages for ping 8.8.8.8 or ping www.google.com, your method always yiels false. The method itself is executed (I know this because I check with E/LogTag). Any idea what the problem might be? I also checked the Android manuscript and there I have allowed INTERNET connections.
  • Alex Mamo
    Alex Mamo over 2 years
    @VanessaF Without seeing it, I cannot say what it could be.
  • VanessaF
    VanessaF over 2 years
    @AlexMamo: Thanks Alex for your comment. I created a new question with additional information to my problem: stackoverflow.com/questions/69953789/…. Maybe you can have a look at this?
  • VanessaF
    VanessaF over 2 years
    @AlexMamo: Could you have a look at my question here (stackoverflow.com/questions/69953789/…). I apply your suggested code but it always returns false. Do you have any idea how to solve this problem?