Sending Firebase Push Notifications via Spring Boot Application — Part 1: Setting Up the Android Application

tahaburak.koc
4 min readDec 20, 2020

Well, hello. I’ve decided to publish a mini-series about sending Firebase Push Notifications via a Spring Boot application therefore I’ll be setting a Firebase Project and an Android application.

If you’ve already set up yours simply skip this one and check out the next parts. Otherwise, you can follow up on this article or check out another article I published to build an Android application with Firebase integration and it requires no coding experience at all.

Let’s get started.

Logo

0. Creating the application

The main focus of the application is going to be displaying push notifications sent from the Spring Boot application that we’ll also be building. So specifications like the application’s name, its minimum SDK version, etc. all up to you. Mine is as follows:

Template: Basic Activity
Name: FirebaseTestApp
Package name: com.tahaburak.firebasetestapp
Language: Kotlin
Minumum SDK: Android 7.0 (Nougat)

1. Git initialization

Well, even if it’s a simple/demo application I prefer to have something valid, accessible. So I wouldn’t miss this part. However, it’s totally optional.

You can obtain the “.gitignore” file from this gist or create a custom one from https://www.toptal.com/developers/gitignore.

The template creates the “.gitignore” file automatically. Simply replace its content with the one you obtained.

In order to initialize Git from Android Studio, you can point the project’s folder with the following steps:

VCS -> Import into Version Control -> Create Git Repository…

Or simply open the terminal and enter the following command:

git init

I’d like to commit the “.gitignore” file first so:

git add .gitignore
git commit -m "created .gitignore file"
git add .
git commit -m "initial commit"

2. Firebase integration

This part requires a couple of steps:

- Creating a new Firebase project and obtaning key file
- Adding dependencies
- A service to obtain Firebase token and listen notification events

2.1. Creating the Firebase Project and obtaining key file

Android Studio provides an assistant that simplifies Firebase integration. Via this assistant, you can create a new Firebase project and integrate your application to that project.

Tools -> Firebase
Cloud Messaging -> Set up Firebase Cloud Messaging
Create the new Firebase project on your browser

When you connect your application you’ll see a new file called “google-services.json” under the “/app” folder. This is the key file that connects your application to the Firebase project.

2.2. Adding the dependencies

With the “Add FCM to your app” step Android Studio will show the following:

build.gradle (project-level)Add rules to include the Google Services Gradle plugin:
classpath 'com.google.gms:google-services:4.3.3'
app/build.gradleApply the Google Services Gradle plugin:
apply plugin: 'com.google.gms.google-services'
Add the library dependency:
implementation 'com.google.firebase:firebase-messaging:20.1.0'

2.3. Creating the service that receives token and listens to notifications

When the application starts FCM SDK generates a token for the device. If you want to send any notification to a specific device or a device group, you should be able to access this token. Also, you need to implement some kind of logic to received notifications.

In order to handle all these let’s create a new Kotlin class called “CustomFirebaseMessagingService” in the “com.tahaburak.firebasetestapp.notification” package.

CustomFirebaseMessagingService.kt

Add the following to your AndroidManifest.xml file so that CustomFirebaseMessagingService can receive data.

AndroidManifest.xml

Let’s run the application and see if it’s able to register the Firebase project and receive a token by sending one from the browser. The result should be like below:

I/ContentValues: FCM token received. Token value: fFDkIbDxQa67WResyO1jPW:APA91bFW4phggV0xrmfrZxN6CI5UxOU1ZbFrSBAxyco8X4ldhI3LJ2wY9ErK67XbXRdBUEVMa7fk_VOU1qZYDyo_xfU6qMeICfZUZMDqaAcgjm2RtiRqB1VFQa6dq4nhxrBAca8-oCEm

D/ContentValues: Message Notification Title: Title
D/ContentValues: Message Notification Body: Text

3. Conclusion

In this article, we’ve created a simple Android application that is able to receive notifications from a Firebase Project. The next one will be about displaying these notifications.

You can find the source code at Github. However, you’ll need to replace the “google-services.json” file with yours :)

Sincerely,
Burak.

--

--