Firebase Push Notification Tutorial

Hardik Dungrani
Dec 26, 2019 · 4 min read

This is a tutorial about sending push notifications to Android through Firebase, based on the new release of Firebase this year (2016).

Android Working With Firebase Push Notification

Firebase is developed in year 2011 by the company know as Firebase. Than Google acquired this product in 2014. Google added many new features into this product and those new features are introduced in Google I/O 2016. Firebase offers so many useful features. But today we are going to discuss regarding Firebase Push Notification .

For sending Firebase Push Notification we are going to use FCM(Firebase Cloud Messaging). FCM is a newer version of GCM(Google Cloud Messaging). FCM includes all features of GCM and enhanced with some new features. Google is not charging anything for FCM service. We can send push notification to single device or group of devices using FCM. Sending messages 2KB limit or Data Messages 4KB limit

How Firebase Push Notification works?

Firebase serves as a module between your server and the devices that will be receiving the push notifications that you create. Your server informs Firebase that a notification has to be sent. Then Firebase does the work behind the scenes to get the notification published.

How to integrate FCM into Android Project?

Step for creating Firebase Project

Step 1: Open Firebase console

Step 2: Click on Add project and give name to your project and select country. If you have already created a project skip this step.

Step 3: Then select “Add Firebase to your Android app”.

Step 4: Add project package id or application id and genrate SHA-1 signatature of your machine.

Step 5: Download google-service.json and put into app module.

How to generate SHA Key using Android Studio:

  1. Open your project into Android studio.
  2. Click on Gradle tab on right side.
  3. Collapse :app module -> Tasks -> Android -> signing report.
  4. SHA-1 will generate. Copy and paste into SHA-1 in firebase console.

How to integrate Firebase SDK into Android Project?

Step 1:

Add below code into <project>/build.gradle file.

buildscript {
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.3.2'
}
}

Step 2:

Add below code into <project>/<app>/build.gradle.

dependencies {
// Add this line
implementation 'com.google.firebase:firebase-messaging:20.0.0'
}
...
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'

Step 3:

Press on sync now in the Android Studio.

Step 4:

After successfully sync. Edit AndroidManifest.xml or download project files.

A service that extends . This is required if you want to do any message handling beyond receiving notifications on apps in the background. To receive notifications in foregrounded apps, to receive data payload, to send upstream messages, and so on, you must extend this service.

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

Note: “FirebaseInstantIdService” is now deprecated. So no need to include FirebaseInstantIdService in manifeast. Now “FirebaseMessaging” handle all task even for token also.

Now you can send Push Notification using Firebase Console

  1. Go to firebase console and select the app you created.
  2. From the left menu select notification.
  3. Click on new message.
  4. Enter message, select single device and paste the token you copied and click on send. The same as I did on the video, and check your device

So that’s all for this Firebase Cloud Messaging Tutorial. If any query/suggestion for Firebase Cloud Messaging Tutorial ask freely on hardikdungrani8@gmail.com

Please go through my GitHub repository.

Thanks.

Hardik D.

Hardik Dungrani

Written by

I have extensive experience in developing innovative and complex Android /iOS Apps and also in Website Developing. Been working in Kotlin for the last 2 years.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade