Firebase Push Notification Tutorial

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 the year 2011 by the company know as Firebase. Then 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 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 the FCM service. We can send push notification to a single device or group of devices using FCM. Sending messages 2KB limit or Data Messages 4KB limit

How does Firebase Push Notification work?

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.

Image for post
Image for post

How to integrate FCM into Android Project?

Step for creating Firebase Project

Step 1: Open The Firebase console

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

Step 4: Add project package id or application id and generate the SHA-1 signature of your machine.

Image for post
Image for post

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

Image for post
Image for post

How to generate SHA Key using Android Studio:

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

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 FirebaseMessagingService. 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 the manifest. 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 the message, select a 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.

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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store