How to connect Android with Firebase Cloud Functions

In this tutorial we are going to see how we can enable Firebase Cloud functions. Firebase Cloud Functions let us run custom logic code in Google supported backend. Firebase functions can also be configured to run on selected triggers like Firebase Database, Analytics.

We can extend Firebase cloud functions to create backend and expose REST apis. In this example we will see how to create and setup firebase functions and call these functions from our Android application.

Setup Firebase Cloud Functions

? Allow Firebase to collect anonymous CLI usage and error reporting information? No

Visit this URL on any device to log in:
https://accounts.google.com/o/oauth2/auth?client_id=…….
Waiting for authentication…

✔ Success! Logged in as …..

Install npm Firebase Tools. Open terminal and run following commands.

In your favorite IDE open project.  If you ask me I prefer to work on visual studio code. Open index.js file and initialize firebase app.

Let’s create our first cloud function. There are two ways to call Firebase cloud functions. First one let you call cloud functions directly from the app. Second way is to call functions by http requests. The format for both of these methods are different.

Cloud Function call via HTTP Requests

Note: The cloud functions are end by calling send()redirect(), or end()functions.

Cloud Function call directly from App

Deploy Firebase Functions

Android Firebase Cloud Functions Setup

Callable Firebase Cloud Functions

Open Android Studio and create new Android project. Add firebase functions dependency in build.gradle

Call cloud function directly by callable firebase function in the code.

Firebase Functions via HTTP request

We will be using volley library to create http requests. Add volley dependency in build.gradle file

Call cloud function via http request

Full Source Code:

Activity Code

activity_cloud.xml

 

One Response

  1. Mike Bleahen July 22, 2019

Leave a Reply