Android

Android Push Notification using Firebase Cloud Messaging.

Firebase Cloud Messaging

Contents

Firebase Cloud Messaging

Implement Android Push Notification using Firebase Cloud Messaging service

Hello Friends we all know about push notifications which inform us about something new service integrated in the app or some new features updated in the existing app.

Most common are Instagram follows, likes and recommendations and it goes the same way for all applications.

Google Cloud Messaging service which is deprecated now because of Google Play services 9.0 update.So now GCM is succeeded by FCM (Firebase Cloud Messaging).
So lets start now.

What is Firebase ?

Firebase is a powerful platform for building Android, iOS and web based applications. Firebase offers real time data storage and synchronization, user authentication, analytics, hosting, database, remote configuration, notification, cloud messaging and lots more. Before the Google I/O 2016, we used Google Cloud Messaging service (GCM) to send data: notification, messages from the server to the clients or android app users. Google introduced the Firebase cloud messaging in Google I/O 2016.

So here we will see the Android Push Notification demo using latest Firebase support.

Note:I assume that you have updated your Android sdk to the latest.

This tutorial will be divided into two parts :

1. Android Studio Project Setup.

2. Project creation on FCM console.

PART 1

Step 1: Create Android Studio Project called as FCM Demo and open its app level build.gradle and it should look like this:

It requires:

com.google.firebase:firebase-messaging:9.0.2

Step 2 :Now open your project level build.gradle and it should have these lines:

classpath com.google.gms:google-services:3.0.0

Step 3:Now we need to include this MyFirebaseInstanceIDService which handles the process of fetching the FCM token from the server:

onTokenRefresh() method contains sendRegistrationToServer(refreshedToken)

and storeTokenToPreference(refreshedToken) methods which do what their name suggest.

Here storeTokenToPreference(refreshedToken) method stores the token once it arrives into Shared Preference (which you need to create) and makes it available throughout the app.

And sendRegistrationToServer() method will send that token to your web service for further processing as per your needs.

Step 4 : Now this class creates notification when FCM message is received on the mobile phone.We will send test FCM message from Notification section of FCM console later on:

Step 5 : Main Activity file which prints the FCM token in the Logcat:

Part 2: Firebase Console part

Step 6: Open https://firebase.google.com/ and click on Go to console at right side and you will see below screen:

firebase_console_home
firebase_console_home

Step 7: After selecting Create New Project below screen will appear, Fill the details as I have filled according to my demo project and click Create Project:

create_project

Step 8: As your project will be created ,you will see the new page which displays your Project name at the extreme left (FCM Demo) in my case, now click Add firebase to your Android app:

add_firebase

Step 9: Below screen will open and fill the package name and SHA1 and click Add App. To get these details follow step 10:

sha1hide

Step 10(Ten): Fetch SHA-1 from Android Studio:

stepsfor_sha1

Step 11: As you complete Add App in step 9 ,automatically google-services.json file will be downloaded with this screen visible to you, copy the google-services.json file to your app directory,as shown in the image below:

copy_json

Step 12: On Click of Continue ,you will be shown console home screen. Click on Send Your First message:

notification

Step 13: Completed the copying google-services.json file ,run the FCM demo app on your mobile, and copy the FCM token.

Step 14:  Paste the FCM token that you have copied from the log in Android Studio, select Single device and click SEND MESSAGE button.

last_stepnotification

Step 15:  check your mobile and you will receive notification like below:

mob

If you have any doubts or suggestions please comment . I will be glad to improve by your suggestions.

You can get the full source code from here:

Remember to include your own google-services.json file to run this demo.

https://github.com/developerhuman12/FCMDemo

Top 10 free Android libraries for app development in android studio

Feeling glad to receive your comment