SetUp Firebase Cloud Messaging in Android - Part 1 - Androhub

SetUp Firebase Cloud Messaging in Android – Part 1

In this article, we are going to learn how to integrate Firebase Cloud Messaging in Android applications.

What is FCM?

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost.

Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention.

For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.

FCM Architecture

FCM relies on the following set of components that build, transport, and receive messages.

FCM Architecture (Credit: https://firebase.google.com/)

VIDEO TUTORIAL

Let’s create one project and start integrating FCM.

Create Project in Firebase

Before doing anything go to the firebase console and create the project by using the following steps:

1. Open the firebase console and login with your Gmail account or create a new account.

2. Now click on the + Add Project button to create a new project.

Add Project

3. After that enter your project name and click on the continue button.

Enter Project Name

4. Then again click on the continue button.

Enable Analytics

5. After that select your Google Analytics Account to which you want to connect and click on the create project button.

Choose Analytics Account

6. It will take some time to create the project and once your project is created then click on the continue button again and it will take you to the project dashboard/console.

Project Created
Firebase Console

Setup Firebase

Firebase can be setup in two ways:

Android Studio

1.  Open Android Studio and navigate to Tools -> Firebase.

Open Firebase Assistant

2. It will open Firebase Assistant containing all list of services provided by Firebase.

List of Firebase Services

3. Find Cloud Messaging and click on it. After clicking on it it will ask you to do 3-4 steps.

4. Firstly, click on connect to Firebase button to connect your project with the firebase project which we created.

Connect to Firebase
Project Connected

5. Once both projects are connected, then come back to Android Studio and it will show connected instead of the connect to firebase button.

Project Connected

By doing this it will add google-services.json file into your app directory.

Google Servies JSON File

6. Secondly click on Add FCM to your app button to add the dependencies to your project. It will open a window to accept the changes.

Accept Gradle Changes

7. After then sync your project and your project is connected with Firebase successfully.

Firebase Console

1. Go to the firebase console and click on the Android icon to connect your project.

Add Android App

2. Then add your android package name and click on the Register App button.

Enter App Details

3. Then it will ask you to download the google-servies.json file. Download the file and put it inside your app directory.

Download JSON File

4. After that click on the Next button and it will ask you to add the dependencies to your project. Add the same dependencies which were added automatically previously.

5. Once the dependencies are added and your project is synced means your project is connected with Firebase successfully.

Token Generation

Firebase cloud messaging works on the token. So we will see how we can generate tokens on the client-side.

1. Create a new java class with the name MyFirebaseMessagingService.java and extend it with the FirebaseMessagingService and override the onNewToken() method.

2. After that declare your Service in AndroidManifest.xml like below:

Here the action com.google.firebase.INSTANCE_ID_EVENT  is responsible to listen to token changes in the device.

3. Inside the onNewToken() method we will be getting the token when the user installs the app the first time. Here we can upload the token to the server and we can store it in preferences for later use.

4. When we will run the app we can see the token in the logs.

Firebase Token in Logs

There are few scenarios when onNewToken() is called:
=> When a new token is generated on the initial app startup.
=> App is restored to a new device or reset the device.
=> User uninstalls/reinstalls the app.
=> User clears app data.

5. Let’s suppose we have to retrieve the token manually. To do that we have to use the below code:

The task will be asynchronous so we have to wait till we receive the token.

Getting Token Manually

Note: To generate the token internet connection is required. If there is no internet connection then no token will be generated.

Thanks. 🙂

 

Post comment

Your email address will not be published. Required fields are marked *