Android Push Notification Using Firebase Cloud Messaging (FCM)

In this tutorial you will learn to implement android push notification using firebase cloud messaging (FCM).

Firebase is a real time cross platform database that provides various functionalities like authentication, storage, notification, etc. Firebase is becoming popular among developers due to its simplicity and easy implementation. In earlier days Google Cloud Messaging (GCM) was used to implement push notification. But Firebase Cloud Messaging (FCM) is better and easy to implement than GCM.

Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Push Notification Using Firebase Cloud Messaging (FCM)

Prerequisites

To use FCM there are few minimum requirements.

  • Device with Android 2.3 or newer and Google Play services 9.6.0 or newer
  • The Google Repository from the Android SDK Manager
  • Android Studio 1.5 or higher version.

 

Android Project

Create a new android studio project with package name com.pushnotificationexample and copy the package name.

 

Firebase Project

Go to Firebase console https://firebase.google.com/console/ to create a new project.

Click on Create New Project button. Enter some project name and your country. Click on Create Project button to create the firebase project.

Android Push Notification Using Firebase Cloud Messaging (FCM)

Choose Add Firebase to your Android app option.

Android Push Notification Using Firebase Cloud Messaging (FCM)

Enter the package name that you copied earlier and click Add App button. You will get a google-services.json file.

Android Push Notification Using Firebase Cloud Messaging (FCM)

 

Add Firebase to Android Project

Select Project view in Android Studio and paste the google-services.json file under app folder.

Android Push Notification Using Firebase Cloud Messaging (FCM)

Open project level build.gradle file and add following line under dependencies.

 

Open app level build.gradle file and add following line under dependencies.

 

Also add following line at the end of app level build.gradle file.

 

Firebase Cloud Messaging (FCM) Implementation

Create a new class MyFirebaseMessagingService that extends FirebaseMessagingService. Add following code inside it.

 

Now we have to add above service in AndroidManifest.xml. So add following code anywhere between <application> <application/> tag.

 

Build and run your app.

 

Sending Push Notification

Go back to Firebase Console and select Notifications option from left menu. Click on New Message button.

Add a message in Message text box. Choose Target as User Segment. This will send the notification to all the users using this android app. You can also send notification to a particular user’s device.

Choose the app package name from drop down menu. Finally click on Send Message button to send the message.

Android Push Notification Using Firebase Cloud Messaging (FCM)

If you have done everything correctly then you will see a push notification in your device.Android Push Notification Using Firebase Cloud Messaging (FCM)

 

Comment below if you are facing any problem in above android push notification tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *