Home Android Android Tutorials Firebase Cloud Messaging ( FCM ) to Send Push Notification Android 2021

Firebase Cloud Messaging ( FCM ) to Send Push Notification Android 2021

2

Welcome to Proto Coders Point. This tutorial will we learn how to send firebase push notifications using Firebase Cloud Messaging free Service (FCM) the easiest way for android 2021.

Final FCM  push notification project demo

firebase push notification project demo done

Sending Push Notifications to Android with Firebase

The 2016 Google I/O announced major improvements to their amazing product – Firebase – a cloud platform with a lot of amazing features for mobile app developers.

One of them is Firebase Cloud Messaging (FCM) — a cross-platform messaging solution that lets users reliably deliver messages at no cost.

Yes, FCM is a free service from Google.

Comparing to the earlier Google Cloud Messaging (GCM), FCM is much more developer-friendly because you don’t even need to see any of the server code involved.

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

This tutorial shows how to set up the skeleton for sending and receiving push notifications via FCM with instructions on the server code.

Video tutorial on the implementation of FCM push notification firebase

Watch this video

Step by Step implementation of firebase push notification in android studio

Let’s Begin implementation FCM firebase push notification in android studio.

Create a new android studio project and name it as FCM push notification 

Step 1 : 

File > New Project > Empty Activity > FCM Push notification (Give Application name)> Package name > Finish

 

Step 2 :

Tools > Firebase 
android studio firebase tool

Your will get a firebase assistant on the right side of android studio window

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost.

Under Cloud messaging you will see text called  Setup cloud messaging  click on it  you need see setup instruction given by Firebase android studio assistant.

Setup Firebase cloud messaging

FCM Connect app to firebase server

Click on Connect to Firebase Button you see under firebase assistant

connect to firebase

After Your app is been Successfully connected to the firebase service you will see a connected text in green color, which means your android application is connected to the firebase server.

firebase connected successfully

Now you need to add FCM dependencies into your project Gradle file ( app level )

Don’t worry all those work will be done for you by firebase assistant you just need to click on add FCM to your app. this will open a window, just click accept changes and all set leave it you firebase android assistant it will do the rest of your work.

firebase FCM Dependencies

 

Firebase have added a messaging dependencies in you gradle file

After Adding the FCM dependencies library the third step is to create a java class that handles Firebase messaging services.

If you wish to do any message handling beyond receiving notifications on apps in the background, create a new Service ( File > New > Service > Service ) that extends FirebaseMessagingService. This service is necessary to receive notifications in foregrounded apps, to receive data payload, to send upstream messages, and so on.

Create a new android services

File > New > service > service ( name it as " Push_Notification_Android"

and now add the following source code to it

Push_Notification_Android.java (service)

package protocoderspoint.com.fcmpushnotification;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import static android.content.ContentValues.TAG;

public class Push_Notification_Android extends FirebaseMessagingService {
    public Push_Notification_Android() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

    }

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
    }
}

The most important concept here is that onMessageReceive is ONLY called when the app is in the foreground, if the app if is background, Google Services will take care of displaying the message

And Finally to get a firebase push notification you need to add INTERNET permission in your AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET"/>

AndroidManifest.xml file 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="protocoderspoint.com.fcmpushnotification">

    <uses-permission android:name="android.permission.INTERNET"/>



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service
            android:name=".Push_Notification_Android"
            android:enabled="true"
            android:exported="true"></service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

There you go, android application ready to get a Push notification from firebase server sent by you manually.

( Bonus )

How to add Default Notification icon with color for firebase push notification? 

Add the following lines of meta data taga,  inside AndroidManifest.xml in between <application> here </application>

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_launcher" />
      
<meta-data 
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/google_blue" />

How to send firebase push notifications to the android app using FCM?

Open Firebase console, Link.

firebase console project

Select your firebase project and open it, In my case, my firebase project name is “FCM push notification” as you can see in the above Image screenshot.

Now the Left side of the firebase console project you will see many services provided by Google Firebase Console.

Then go into Grow option drop-down list you will see Cloud messaging. open it you may see a window something like the below image.

firebase console project

Click on send your first message

Now firebase push notification is ready to be sent

  • Enter Notification Title
  • Enter Notification Text
  • Select target package name
  • Review and send
send firebase push notification

Notification will be displayed on your Android Phone, and if you tap on that notification it will open the app

Related articles

flutter firebase integration – connect flutter app to firebase console

flutter push notification

send push notification to android using php firebase

flutter getx authentication – firebase auth

2 Comments