Android Push Notification using Firebase


Firebase has tools and infrastructures to build the app. It contains a lot of features, in this blog we will take a look at how to setting up Android application for receive notification from Firebase Cloud Messaging.

If you are using Google Cloud Messaging, Google recommended to upgrading FCM, because FCM is a new version of GCM.


First, go to Firebase console, log in your account then click to create a new project, fill your project name and region.


Screenshot 2016-05-29 21.57.01

Click add Firebase to your Android app.

Screenshot 2016-05-29 21.57.44

Add your package name and debug signing certificate SHA-1.

You can see your SHA-1 by run following command.

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android 

Screenshot 2016-05-29 22.10.30

Your bowser will download google-services.json. It is a google service configuration. Move it into root directory in your app module.

Screenshot 2016-05-29 22.10.47

Add Google Service plugin in build.gradle in root of project, and sync your Gradle.

Screenshot 2016-05-29 22.11.15

Now, we’re finished setting up in Firebase console. We will move to next step, set up your Android project.

Screenshot 2016-05-29 22.12.25

Add firebase messaging dependency in build.gradle in app module. And sync Gradle.

dependencies {
  // ...
  compile ''

Add FirebaseInstanceIdService for get FCM instance ID token from Firebase Cloud Messaging Server.
The onTokenRefresh method called if token is updated.
When you get token from FCM. You can use it for persisting to your server.

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);


    private void sendRegistrationToServer(String token) {
        // TODO: Send any registration to your app's servers.

Add FirebaseMessagingServer for receive notification message from FCM.
The onMessageReceived called when the message is received. You have to handle the message from FCM here. Such as; Create and show notification bar.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Create and show notification

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("FCM Message")

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */,;

After that add the both classes as a service in AndroidManifest.xml.
The both services are running on background.

     <action android:name=""/>

     <action android:name=""/>

Now, you are finished to set up the Android application.
Build and run it.

You will see FCM InstanceID token in logcat. Because we log it in onTokenRefresh method. Copy it.

Screenshot 2016-06-02 23.48.03

New FCM feature that GCM does not have, that is we can send notification message directly from Firebase console, that’s cool! right?

Open Firebase console and select Notification menu on the left-hand side.

Screenshot 2016-06-05 22.12.58

Look at the target section. We can send the message to device three ways.

First, send to all devices in a specific application.

Screenshot 2016-06-05 22.21.44

Second, send to a device that subscribe to the topic.

Screenshot 2016-06-05 22.38.17

In the first time, your project does not have any topic. You need to subscribe the topic in your project.

By call following method.


waiting about one day delay between new topic creation, it will be available in your Firebase console.

Screenshot 2016-06-07 00.18.53

And the last, send to a specific device by FCM registration token, that show in your logcat above.

Screenshot 2016-06-04 01.35.45

In Advance options, you can custom your message title and can attach custom data (key/value pair). Also, can set a priority of the message and enable/disable sound when your device receives the message. And you set how long of the message that should be kept for resending, default are 4 weeks.

Screenshot 2016-06-04 01.36.26

You can handle the custom data when notification message is tapped. Custom data of notification message is available in the intent extras.

    if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            String value = getIntent().getExtras().getString(key);
            Log.d(TAG, "Key: " + key + " Value: " + value);

Above code we check that custom data is exist or not, then for each all key of custom data, then get value of data by key.

Send first notification message

Put your message text and click send the message.

Screenshot 2016-06-05 22.20.40

Add some custom data, then click SEND MESSAGE.

Screenshot 2016-06-07 00.50.41

Waiting a few minutes, you will receive notification on your Android application.

Screenshot 2016-06-05 22.24.57

Tap it! The custom data will show up in Logcat.

Screenshot 2016-06-07 00.52.17

Yeah!, now we completed setup Android application to receive notification from Firebase console. You can download source code on Github.

Reference: Firebase document

8 thoughts on “Android Push Notification using Firebase

  1. hi there if i have multiple notification send in background so how do i save them all in my desired location (i.e: database /list) ,by only clicking on one notification of the many background notification sent on the device by the server which are in the notification/system tray

  2. Very useful thank you.
    I’m trying just to get the simple notification going so I added only the firebase-core dependency but the
    notification does not show up … did you have a similar problem, or any idea what I’m missing, thank you!

  3. sir great tutorial there
    but im getting error, it says background sync failed: AUTHENTICATION_FAILED
    any help please?

  4. Hey I am so happy I found your weblog, I really found you by mistake, while I was browsing on Askjeeve for something else, Anyways I
    am here now and would just like to say thank you for a incredible post and a all round entertaining
    blog (I also love the theme/design), I don’t have time to go through it all at the minute but I
    have book-marked it and also included your RSS feeds, so
    when I have time I will be back to read a lot more,
    Please do keep up the awesome b.

  5. Sir can you please describe how to turn on and off the notification using the image switchers?

Comments are closed.