Android Call Cloud Functions for Firebase

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.

NOTE: Refer Kotlin Convert Hashmap to Object (Kotlin Serialization).

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.