Android Call Cloud Functions for Firebase

April 28, 2019
using Cloud Functions for Firebase client SDK

We can call Cloud Functions for Firebase from Android using Cloud Functions for Firebase client SDK.

If you are using Google Cloud Functions, you can make Google Cloud Functions compatible with Cloud Functions for Firebase.

You also make HTTP request to Cloud Functions for Firebase without using Cloud Functions for Firebase client SDK.

NOTE: Cloud Functions for Firebase client SDK provide some convinience and intergration with other Firebase services like Firebase Authentication.

Include dependencies.

dependencies {
    implementation 'com.google.firebase:firebase-functions:16.3.0'
}

Code

NOTE: Assume test_firebase_function expect a name parameter and return a name response as well.

fun getFirebaseFunction(): Task<String> {
    val functions = FirebaseFunctions.getInstance()

    val data = mapOf(
        "name" to "Desmond"
    )

    return functions.getHttpsCallable("test_firebase_function")
        .call(data)
        .continueWith { task ->
            val result = task.result?.data as HashMap<*, *>
            result["name"] as String
        }
}
getFirebaseFunction().addOnCompleteListener { task ->
    if (task.isSuccessful) {
        val name = task.result
        Timber.d("name=$name")
    }
    else {
        Timber.e(task.exception)
    }
}

NOTE: There might be some firebase prerequisites.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.