Android cloud function call through Firebase

Situation

I created the Google Cloud Function through functions.https.onRequest , which works great when I paste its URL into the browser and integrates well with my Firebase installation. This function is meant as some API method opened from the backend, which I would like to call from clients. In this particular case, the client is an Android application.

Question

Is there any way to make an HTTP request for Cloud Function by calling it through Firebase? Or will I have to do a manual HTTP request?

+16
android firebase google-cloud-functions
source share
4 answers

Starting with version 12.0.0, you can call the cloud function in a simpler way

Add the following line to build.gradle

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

And use the 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 safely use it in the main thread. Callbacks are also triggered in the main thread.

+18
source share

fireman 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. Your Android application does not have an SDK for calling Google cloud functions. I would definitely request a function .

But for now, this means that you should use the usual ways to call HTTP endpoints from Android:

+10
source share

While this is not possible, but as mentioned in another answer, you can run functions using an HTTP request from Android. If you do, it is important to protect your functions with an authentication mechanism. Here is a basic example:

 'use strict'; var functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.helloWorld = functions.https.onRequest((request, response) => { console.log('helloWorld called'); if (!request.headers.authorization) { console.error('No Firebase ID token was passed'); response.status(403).send('Unauthorized'); return; } admin.auth().verifyIdToken(request.headers.authorization).then(decodedIdToken => { console.log('ID Token correctly decoded', decodedIdToken); request.user = decodedIdToken; response.send(request.body.name +', Hello from Firebase!'); }).catch(error => { console.error('Error while verifying Firebase ID token:', error); response.status(403).send('Unauthorized'); }); }); 

To get the token in Android, you must use this one and then add it to your request as follows:

 connection = (HttpsURLConnection) url.openConnection(); ... connection.setRequestProperty("Authorization", token); 
+6
source share

Yes it is possible:

  1. Add this to the app / build.gradle file :

    implementation of 'com.google.firebase: firebase-functions: 16.1.0'


  1. Initialize Client SDK

    Private FirebaseFunctions mFunctions;

    mFunctions = FirebaseFunctions.getInstance ();


  1. Call function

     private Task<String> addMessage(String text) { Map<String, Object> data = new HashMap<>(); data.put("text", text); data.put("push", true); return mFunctions .getHttpsCallable("addMessage") .call(data) .continueWith(new Continuation<HttpsCallableResult, String>() { @Override public String then(@NonNull Task<HttpsCallableResult> task) throws Exception { // This continuation runs on either success or failure, but if the task // has failed then getResult() will throw an Exception which will be // propagated down. String result = (String) task.getResult().getData(); return result; } }); } 

Link: Calling Firebase Cloud Functions

0
source share

All Articles