如何检查firebase服务是否可用?

时间:2016-07-14 12:27:54

标签: android firebase

如果应用程序在防火墙后运行或网络中断或某种审查,我们如何使用代码检查应用程序是否可以访问firebase系统?

3 个答案:

答案 0 :(得分:4)

我猜你现在无法通过应用程序内的代码检查Firebase是否可用。但是,您可以通过查看Firebase状态信息中心来检查Firebase本身是否正常运行。

在此网站中,您还可以找到Firebase服务在过去的可用日期或不稳定日期。

希望它在某种程度上有所帮助。

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

答案 1 :(得分:2)

您应该检查Google Play服务是否可用:

/**
* 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.
*/
public static boolean 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");
        return false;
    }
    return true;
}

您还需要将以下行添加到build.gradle

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

答案 2 :(得分:0)

作为

FirebaseApp.getInstance() throws IllegalStateException 

您可以尝试以下解决方案

private val isFirebaseEnabled: Boolean
    get() {
        return try {
            FirebaseApp.getInstance() != null.   //that is always true, nevertheless it will throw exception if Firebase is not available
        } catch (e: IllegalStateException) {
            false
        }
    }

很遗憾,它不会检查实际的网络限制。您可以尝试ping FCM并捕获连接超时。例如

private fun isInternetAvailable(): Boolean {
    return try {
        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
    }
}