Android Push Notification Using Firebase

Firebase is a powerful platform for building mobile and web based applications. It helps developer to build high-quality apps and games. Firebase provides facilities of  real time database and synchronization, user authentication service, analytical tools, hosting web site, send notification and many more. Before the Google I/O 2016 event, we had worked with  Google Cloud Messaging service to send messages or notifications from the server to the clients or mobile app users. In Google I/O 2016, Google introduced the firebase cloud messaging.

Steps for sending message using firebase.

Step 1:

First of All, we create project on google firebase console, Here is the link of it: https://console.firebase.google.com/

image00

Add project name and country name in new project. After the firebase dashboard is visible to you, select appropriate technology. We needed for android so we clicked android app icon.

image04

After that, below screen will be visible. Add package name of new project and SHA-1 key to the Project.

image03

Once you submit all the information, firebase gives you google-services.json which contains all information regarding your project. Copy this file and paste this file into your app directory e.g. //app/(paste here).

image02

Step 2:

Now let us start the coding part. After creating new project, copy google-service.json file. Add google play services. Open project level build.gradle file and place below line and sync project.

// google services
classpath ‘com.google.gms:google-services:3.0.0’

Example:

Step 3:

Now open your app level build.gradle file and paste below line

  • add apply plugin: ‘com.google.gms.google-services’
  • compile ‘com.google.firebase:firebase-messaging:9.0.2’

press Sync now.

Example:

Step 4:

Create user registration service that extends by FirebaseInstanceIdService and override onTokenRefresh method which fire when new token is generated. It contains token and token helps to send notification. If we want to send a particular notification to a particular user then token will help to identify users.

Sample code is below :

Step 5:

Create message service that extends FirebaseMessagingService which received message from firebase and display to the user. It is required to override onMessageReceived method with RemoteMessage object. So RemoteMessage give notification object for accessing notification in App.

Sample code is below :

Step 6:

Declare all services in manifest file with permissions.

Sample code is below :

Step 7:

Now run application and test notification message. For Testing purpose, Firebase provided facilities of it. Look in the below screen, click it is your project structure in firebase. Click Notifications and get console of sending notification for testing. Just type message, Select App and send it. Enjoy…!

image06

Send Message Screen :
image05

Step 8:

Finally got Notification in Application . Yuppy ….!!!

image01

Sachin Patel

Sachin Patel | Android Developer

I am an Android Developer at Yudiz Solutions Pvt. Ltd. - a mobile App Development company. I am passionate about android app development and like adopting new technologies to maximize development efficiency and produce innovative applications.
mm

Sachin Patel Android Developer

I am an Android Developer at Yudiz Solutions Pvt. Ltd. - a mobile App Development company. I am passionate about android app development and like adopting new technologies to maximize development efficiency and produce innovative applications.

Comments are closed.

Top