Skip to content Skip to sidebar Skip to footer

How To Check If Firebase Service Is Available?

In cases when the app is running behind a firewall or there is a network outage or some sort of censorship, How can we check using code to see if the app can access the firebase sy

Solution 1:

I am guessing currently there is no way you can check whether Firebase is available or not from codes inside the app. However you can check if Firebase itself is working fine by checking Firebase Status Dashboard.

In this site you can also find in what date Firebase service was not available or unstable in the past.

Hope it helps in some way.

https://status.firebase.google.com/

Solution 2:

You should check if Google Play Service is available like this:

/**
* Check the device to make sure it has the Google Play Services APK. If it
* doesn't, display a dialog that allows users to download the APK from the
* Google Play Store or enable it in the device's system settings.
*/publicstaticboolean checkPlayServices(Context context) {
    int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable
            (context);
    if (resultCode != ConnectionResult.SUCCESS) {
        Log.i(TAG, "This device does not support Google Play Services. " +
                "Push notifications are not supported");
        returnfalse;
    }
    returntrue;
}

You need to add the following line to your build.gradle also.

compile 'com.google.android.gms:play-services-gcm:11.8.0'

Solution 3:

As the

FirebaseApp.getInstance() throws IllegalStateException 

you my try below solution

privateval isFirebaseEnabled: Booleanget() {
        returntry {
            FirebaseApp.getInstance() != null.   //that is always true, nevertheless it will throw exception if Firebase is not available
        } catch (e: IllegalStateException) {
            false
        }
    }

unfortunately it won't check actual network restrictions. You may try to ping FCM and catch connection timeout. E.g.

privatefunisInternetAvailable(): Boolean {
    returntry {
        val connection = URL("https://theUrl.com").openConnection() as HttpURLConnection
        connection.setRequestProperty("User-Agent", "Test")
        connection.connectTimeout = 10000
        connection.connect()
        connection.disconnect()
        true
    } catch (e: Exception) {
        false
    }
}

Post a Comment for "How To Check If Firebase Service Is Available?"