Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a Cloud Function from Android through Firebase

Situation

I have created a Google Cloud Function through functions.https.onRequest, which is working nicely when I paste its URL in a browser and integrates well with my Firebase setup. This function is meant as somewhat of an API method exposed from the backend, which I'd like to invoke from clients. In this particular instance, the client is an Android app.

Question

Is there any way I can perform the HTTP request for the Cloud Function by invoking it through Firebase? Or will I have to still perform a manual HTTP request?

like image 531
tvkanters Avatar asked Mar 18 '17 09:03

tvkanters


2 Answers

Since version 12.0.0 you can call cloud function in more simple way

Add following line in your build.gradle

implementation 'com.google.firebase:firebase-functions:19.0.2'

And use following code

FirebaseFunctions.getInstance() // Optional region: .getInstance("europe-west1")
    .getHttpsCallable("myCoolFunction")
    .call(optionalObject)
    .addOnFailureListener {
        Log.wtf("FF", it) 
    }
    .addOnSuccessListener {
        toast(it.data.toString())
    }

You can use it on main thread safely. Callbacks is triggered on main thread as well.

You can read more in official docs: https://firebase.google.com/docs/functions/callable

like image 113
Dima Rostopira Avatar answered Oct 10 '22 11:10

Dima Rostopira


firebaser here

Update: There is now a client-side SDK that allows you to call Cloud Functions directly from supported devices. See Dima's answer for a sample and the latest updates.

Original answer below...


@looptheloop88 is correct. There is no SDK for calling Google Cloud Functions from your Android app. I would definitely file a feature request.

But at the moment that means you should use the regular means of calling HTTP end points from Android:

  • Make an HTTP request with android
  • Android: AsyncTask to make an HTTP GET Request?
  • Http Get using Android HttpURLConnection
  • Android's HTTP Triggers documentation
like image 32
Frank van Puffelen Avatar answered Oct 10 '22 09:10

Frank van Puffelen