Technology

How To Send Firebase Push Notifications In Android?

How To Send Firebase Push Notifications

In this technologically advanced world, Google is one of the front runners in the internet world. One of the excellent services from Google is firebase. Firebase is described as a platform on cloud technology known for leveraging striking features for mobile application developers. This cloud platform consists of some fantastic models, and one of the modules is firebase cloud messaging.

Before getting into the depth of this, we want you to know that it is a guide containing the process of sending push notifications on the Android platform. The recent release of firebase enables the user to send and receive information using cloud messaging. This excerpt will empower you to set up the server process of sending and receiving notifications using the firebase server in the android app. Before that, let us know some fundamentals of the firebase.

What is Firebase?

Firebase is one of those exotic services from Google for mobile application development. It is a service that helps the developer to develop, improvise, and grow mobile applications. It encloses a software development kit for leveraging various development options for the developer. It allows you to create collaborative applications, whether secure access to the database straight from the client-side code.

How to push notifications using Firebase in Android?

Using firebase now, you are going to push the notification function in any Android application. The replacement of the default push notification provider will occur using an app that utilizes Google cloud messaging service which you can learn more about https://techyhost.com/. First of all, you need to connect your Android application from the firebase server. For that, you need to have the latest Android studio and a firebase account. This approach can be categorized into a few steps, which are as follows:

1. Adding the Firebase service to your project

To add firebase cloud messaging to your application, you have to go to the tools, select the firebase from the dropdown menu, select cloud messaging, and click on the setup firebase cloud messaging option.

2. Attaching the Google services JSON file

The second step is replacing the Google services JSON file in your app’s folder, possessing the name “google-services.json.” The JSON file you have downloaded will be loaded into your application using a plugin named Gradle.

3. Configuration of Gradle files

Gradle files will utilize the Google services plugin. To do that, you have to open the Android studio and modify the gradle file named build. gradle. This step consist of a few sub-steps, which are as follows:

I. Modify the project-level build. gradle file

This gradle file is located in the project folder of your application, and you have to append the following line in the build.gradle file:

You need to add the below lines:

buildscript{
dependencies
 {
classpath ‘com.google.gms.google- service: 3.0.0’
 }
}

II. Append some line of code into the app-level build.gradle
This file is present in the project or app module in the project.

A. Add a line in the bottom
apply plugin: ‘com.google.gms.google-services’. This needs to use in the build.gradle file.

B. Add the dependencies related to firebase
In the same build.gradle file, you need to append the firebase dependencies.

dependencies
{
   compile ' com.google.firebase: firebase-core: 9.2.0’
   compile ‘com.google.firebase:firebase-messaging:9.2.0’
}

C. Updating the services utilizing com.google. Android.gms:play-services

If I am going to add firebase into an already existing project with the function of gms: play services like GPS location, you need to update the recent versions. You have to find out the correct and recent version of the play services presently.

compile ‘com.google.android.gms: play-services-location:9.2.0’
compile ‘com.google.android.gms: play-services-places:9.2.0’

D. Addition of application ID into the default configuration section

In the defaultConfig section, you have to add the application. The code is as follows:

android {
 
    defaultConfig {
    applicationID "com.example.my.app"  // your app id
                }
}
4. Addition of services into the app

To use the firebase cloud messaging service, you have to add two services, which are as follows: To test the push notification service, you have to add a necessary testing code and other codes to maintain the synchronization of messages in your application.

A. A service to extend FirebaseMessagingService
Since you want to receive various notifications in your application, that is why you will have to append service for extending the FirebaseMessagingService code mentioned below:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
 private static final String TAG = "FCM Service";
 @Override
                public void on message received(RemoteMessage remote message) {
        //It handles FCM messages.
        //the data and notification can be handled here if the application is running in the foreground. her
        // Additionally, the generation of your notification can be initiated in this segment.
 
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
                }
}

Meanwhile you have to add it in the AndroidManifest.xml file like this:

<service android:name=".MyFirebaseMessagingService">
          
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

B. A service to extend FirebaseInstanceIDService
This service is known to modify the method for association of the FCM InstanceId of the user. The code is mentioned below:

public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";
    @Override
    public void onTokenRefresh() {
       
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
 
                
        sendRegistrationToServer(refreshedToken);
                }
    private void sendRegistrationToServer(String token) {
                }
}

You also have to add it to the AndroidManifest.xml file to verify the loaded service.

<service android:name=".FirebaseIDService">
          
<intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
5. Now, test to send the first notification in your application

For verifying the setup process, you have to run a test by sending a text message on your own mobile. For that, you have to select the notification option present in the grow section from the left corner of the firebase screen.

This is the whole setup process of pushing notifications on the applications using firebase services. If you receive any error messages the most, it will be the compilation problems. If there is a message like a firebase crash, then it is alright because we aren’t using Firebase Crash Analytics services.