How to add Push notification in Android using Firebase (cloud messaging)

1 205

Push notification in Android using Firebase

Before explaining you about Push Notification let me define what is Firebase Cloud Messaging is. FCM previously called GCM (Google Cloud Messaging) is a path used for sending messages and notifications, without any cost, for Android, Web Applications, and IOS. It is company owned by Google, Firebase that provides this service. We can implement Push notification in Android using Firebase cloud messaging. FCM has introduced remarkable new features that are more effective in sending a Push notification, then by sending it using GCM. Firebase provides advanced features such as Authentication, Reliability, Real-time Database, Hosting, and Cash Reporting and provides battery efficient connection channel.

WHAT IS PUSH NOTIFICATION?

Push Notification, is the transmission of information (messages) from an Application to a computing device, without asking or receiving the request from a client. The Push Notifications are also called Server Push Notification. We use Push notification when a new event occurs and we have to send data from our server to our application. A big benefit of using Push notification is that you can receive text message alerts even if your Android Smartphone screen is locked and this process is proficient because it constantly connects to the server using pull method, asking there are new events. A message and a customized application icon appear in the status bar when a user receives a Push notification. There are different formats of sending a Push Notification, for example, we can send data to a single device, to a group of devices (to give specified information). We can also send messages from client Applications

Project  Setup

First, we need to setup our firebase project to start work on Push notification,we will create firebase account to access Firebase console.Click on CRATE NEW PROJECT

https://console.firebase.google.com

Push notification in Android

2. After that enter your Project name and Country/region and click CREATE PROJECT 

Push notification in Android

3. Then in firebase console, you will see different firebase services scroll down and  get Notifications tab and click on GET STARTED 

Push notification in Android

4. Now click on android icon

Push notification in Android

5. In this box you will provide your android app Package name you have to use the same package name in Android Studio and in the Firebase console then click on ADD APP

Push notification in Android

6. After clicking on ADD APP you will get a JSON file to add into your android app project as shown in pic below. after that click on CONTINUE 

Push notification in Android

7. Now we have to add Firebase to our app and modify gradle files as pic shown below

Push notification in Android

Implement Android Firebase push notification

1. Project-level build.gradle (/build.gradle):

2. App-level build.gradle (//build.gradle):

includes Firebase Analytics by default help_outline

3. Finally, press “Sync now” in the bar that appears in the IDE

We will declare this service in the AndroidManifest.xml

In Main activity we have a Button to get the current token. Why I used token? because the token is important  we use it in the Firebase console to set the right destination for our device.

We will monitor the token creation process and get notified when it is available using a custom service that extends FirebaseInstanceIdService. we override the onTokenRefresh method.

Here we will  implement the service that handles incoming push notification.

 

Comments

comments

  1. […] Source : Push notification in Android […]

Leave A Reply

Your email address will not be published.