Blog Infos
Author
Published
Topics
Published

Cloud Functions for Firebase is a tool that lets you run code automatically on a server without managing it, when certain things happen in Firebase or when someone sends a request through the web.

Photo by Annie Spratt on Unsplash

In this tutorial we’ll be talking about Cloud Functions v2.

There are two types of functions:

  • HTTP functions: A normal function that can be run by calling an HTTP endpoint.
  • HTTP callable functions: Similar to an HTTP function but handles authentication for you. This is really useful if you’re already using Firebase Authorization in your project. To use it, you’re required the use the Firebase Functions SDK on Android.
Setup Cloud Functions in Firebase:

To learn how to setup the project you can follow the tutorial in Firebase’s website.

Write Cloud Functions:

Now that you have your project working let’s write a simple function that accepts a name and returns a greeting.

import * as functions from "firebase-functions/v2/https";

export const hello = functions.onCall(async (request) => {
    const name = request.data.name
    return {
        "response": `Hi ${name}`
    }
})

To deploy the function use:

firebase deploy --only functions

Make sure your function is deployed correctly before proceeding to the next step.

Call Cloud Functions from Your App:

Now that you have your function, let’s call it from your app. Remember that HTTP callable functions require you to be authenticated so first make sure you somehow authenticate with Firebase Authentication.

Start by including the dependency

implementation platform('com.google.firebase:firebase-bom:32.3.1')
implementation 'com.google.firebase:firebase-functions-ktx'

Now let’s call the function

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

No results found.

Firebase.functions.getHttpsCallable("hello").call(
    mapOf("name" to "Victor)
).addOnSuccessListener {
    // handle success
}.addOnFailureListener { exception ->
 	// handle error
}

If you’re using Coroutines in your project you can wrap the call in suspendCancellableCoroutine.

suspendCancellableCoroutine { cont ->
    Firebase.functions.getHttpsCallable("hello").call(
        mapOf("name" to "Victor")
    ).addOnSuccessListener {
        // parse your data based on what the function returns
        val data = result.data as? Map<String, Any> ?: run {
            cont.resumeWithException(IllegalStateException("Invalid response"))
            return@addOnSuccessListener
        }
        cont.resume(data["response"] as String)
    }.addOnFailureListener { exception ->
        cont.resumeWithException(exception)
    }
}

That way you can make this function suspend on Kotlin and call it normally from your view model/use case.

If you go ahead and run this code you’ll see that it returns “Hi Victor”.

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
This is the second article in an article series that will discuss the dependency…
READ MORE
blog
Let’s suppose that for some reason we are interested in doing some tests with…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu