Mobile App Push Notification with Firebase Cloud Functions and Realtime Database

In this article you will learn how to send push notifications to your Android application using Firebase Realtime Database and Cloud Functions for Firebase. In the example a change to a specific node in Realtime Database will trigger a push notification and display a notification on the user’s screen.

Some use cases might be:

  • new message in a chat
  • new topic in a forum

Let’s get started!

Preparing the Android Application

Of course, you will first need to create a Firebase project and connect your application to it. For simplicity, in this article we will not discuss the basic connection to Firebase of an Android app. In order to implement the push notification functionality you need to connect Firebase Cloud Messaging to the app. The easiest way to do this is to use the Firebase Assistant from the Tools menu in Android Studio. In the assistant, select Cloud Messaging and follow the steps on screen. It will automatically import and sync your project’s Gradle file.

The next step is creating a service that extends FirebaseMessagingService and specifies what happens when a message is received. Below is an example of this.

Local notifications

Next, this service needs to be registered in the Manifest.xml file.

Don’t forget to subscribe to your topic in an activity. Instead of pushNotifications you can use whatever topic name you want, just use it in the script for the Cloud Function (see in the last paragraph) as well.

FirebaseMessaging.getInstance().subscribeToTopic("pushNotifications");

Setting up Firebase Cloud Functions

Firstly, Firebase CLI (Command Line Interface) requires Node.js and npm. I recommend the version of Node.js that says Recommended for most users on their website- https://nodejs.org/en/.

Install the Firebase Tools. The easiest way to do this is opening a command prompt and navigate to the folder where you want to setup this service.

npm install -g firebase-tools

Afterwards you have to login to Firebase using the following command.

firebase login

This command will open a browser and will let you authenticate to your Firebase account. At the end of this process you will receive a message to let you know that the operation went well.

If you want to log out and choose another account you can use the following command.

firebase logout

After login, use the following command in order to initialize Cloud Functions. It will prompt a message like “You’re about to initialize a Firebase project in this directory: your chosen directory”. Choose “Yes” when asked if you are ready to proceed.

firebase init functions

This will prompt a dialogue to let you choose from several options using the arrows and the enter key. I suggest using an existing project.

After selecting the project you’ll be asked to choose a language to write the Cloud Functions, I went for Javascript.

Afterwards you’ll be asked if you want to install dependencies with npm? Choose yes. And with that, Firebase initialization is done.

Writing Code for the Cloud Function

Check your folder where you’ve done all this work and you will see a sub-folder called functions. Inside you’ll find the index.js file where you will write your code for Firebase Functions. I will use it as a trigger for when I make a change to a specific node in the database.

The notification is fired when a node is added or changed in the path you’ve given

What does this function actually do? Every time something is written into the specified node the value is caught with change.after.val() and saved into an object. After saving the object a new notification is created with values from that object as title and body. In this example I’ve used the title and message properties to the object, but they could’ve also been other properties as strings. The important thing is that the property name from the database to be written exactly here, like title or message it could also be valueObject.imageUrl or valueObject.firstName.

When the code part is done you must deploy it to the server using this command:

firebase deploy

or

firebase deploy --only functions

You can check the log messages of your project by navigating to the Functions section, and selecting Logs in the main area of the window.

Conclusion

Cloud Functions are a great way to access backend functionality using Firebase and require little setup and no server of your own!

Have fun with it!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store