How To Send Firebase Push Notification From App Server Tutorial

How to send Firebase push notification to your Android or iOS client from application server example.

I will give you an example of How to send firebase push notification to Android or iOS devices from PHP, Java,  C# or using Rest Client.

If you directly need source code find Github link at the end of this article.

Here is Rest API example which we will use to send notifications to our client application. You can implement this Rest API in your application server.



Header :

Content-Type: application/json

Authorization: Server Key

(e.g key=AIzaSyD_Vw3Zf8XXXSDMSDKLMGyqR_Kyw5o)

You can find Servery key under Settings >Cloud messaging > Server key

Body :

You can send two types of data in request body.

1. Notification payload. (if you send notification using firebase console. it also send notification payload. Firebase SDK at client side do not handle or receive push notification from server. instead notification is passed to system for displaying in notification panel.)

2. Data payload. (If you want to handle notification in your client application you should send data payload)

You can read about difference in notification payload and data payload from this

We will send data payload.


{ “data”: 
 “title”: “Firebase notification”,
 “detail”: “I am firebase notification. you can customise me. enjoy”,
 “to” : “efaOvIXDbik:APA91bEkNUVWNaoA….”

to = your application firebase token.

where to get this firebase token ?

•On Initial startup of project Firebase SDK will generate a unique device token. override method onTokenRefreshed() will be called whenever a token is updated or generated.

and you have to save this token in your application server.

Above example is to send notification to single device.

Here is a rest client screen shot for more clarification.

Sending Firebase Push Notification from server

How to Send push notification to all users or multiple devices from server side ?

We already discussed how to send push notification to single device.

Now the second method is Topics. Topics are just keys which users subscribe to. Users have to opt to any topic/ subscribe to any topic. Users can subscribe to any existing topic or can create new topic and than subscribe to it.

To subscribe to any topic. users/client use this code (this is Android Code)


HTTP POST REQUEST SAMPLE for send firebase push notification to all devices from server.

Body :
  "to": "/topics/anytopic",
  "data": {
    "message": "This is a GCM Topic Message!",

You can implement this code in your application server.

Now all users/ devices who subscribed to “news” topic will receive push notification.

Now we will write Java client code which will send FCM Push Notification and you can use this code in your application server.

Step 1-

First of all create new project in your IDE (I used Intellij IDEA).

Step 2- Setup Gradle dependencies

Add below dependencies in your project.

  1. Javax Annotation
  2. Google Gson
  3. Apache HttpClient
group 'pushnotification'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile ''
    compile 'org.apache.httpcomponents:httpclient:4.5.4'


Step 3- Create Model / POJO classes for FCM Notification Data payload

import javax.annotation.Generated;

public class NotificationData {

    private String mDetail;

    private String mTitle;

    public String getDetail() {
        return mDetail;

    public void setDetail(String detail) {
        mDetail = detail;

    public String getTitle() {
        return mTitle;

    public void setTitle(String title) {
        mTitle = title;


import javax.annotation.Generated;

public class NotificationRequestModel {

    private NotificationData mData;
    private String mTo;

    public NotificationData getData() {
        return mData;

    public void setData(NotificationData data) {
        mData = data;

    public String getTo() {
        return mTo;

    public void setTo(String to) {
        mTo = to;

Step 4- Sending Firebase Push Notifications API using Java Http Client

Create a new Application Class inside main package of your project.

import model.NotificationData;
import model.NotificationRequestModel;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.lang.reflect.Type;

public class ApplicationClass {

    public static void main(String[] args) throws IOException {

        System.out.println("Welcome to Developine");

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(

        // we already created this model class.
        // we will convert this model class to json object using google gson library.

        NotificationRequestModel notificationRequestModel = new NotificationRequestModel();
        NotificationData notificationData = new NotificationData();

        notificationData.setDetail("this is firebase push notification from java client (server)");
        notificationData.setTitle("Hello Firebase Push Notification");

        Gson gson = new Gson();
        Type type = new TypeToken<NotificationRequestModel>() {

        String json = gson.toJson(notificationRequestModel, type);

        StringEntity input = new StringEntity(json);

        // server key of your firebase project goes here in header field.
        // You can get it from firebase console.

        postRequest.addHeader("Authorization", "key=AAAA7-UgB34:APA91bFhpZ1-MYyFfQd2gof0vUAFKNcQCmmf_10acMn-HS_0iBBvP");

        System.out.println("reques:" + json);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatusLine().getStatusCode());
        } else if (response.getStatusLine().getStatusCode() == 200) {

            System.out.println("response:" + EntityUtils.toString(response.getEntity()));

Simply Run this code and it will send Firebase Push Notification to your Android/iOS application using Firebase Server.

This will be output of above Java code.

"C:\Program Files\Java\jdk1.8.0_77\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5
Welcome to Developine
request:{"data":{"detail":"this is firebase push notification from java client (server)","title":"Hello Firebase Push Notification"},"to":"eHp6V2Wtr4I:APA91b4tPNStf-vFTGNy8RSbZKz"}

I copied token of single user manually and used in above code. In real project you have to send Firebase token which is generated on client side (Mobile App) to your application server.


We first learned how we can send Firebase push notification using Rest Client. we also learned how to send Firebase Push Notification from java client using firebase server.

Recommended Reading:-

How to setup firebase push notification in Android

If this helped you share this article and subscribe to our website for latest updates on Kotlin, Android, Spring Boot.

7 thoughts on “How To Send Firebase Push Notification From App Server Tutorial”

  1. how to pass data like employee id and name in data.?

    { “data”:
    “title”: “Firebase notification”,
    “detail”: “I am firebase notification. you can customise me. enjoy”,
    “to” : “efaOvIXDbik:APA91bEkNUVWNaoA….”

    1. Hi Mano sorry for late reply. you are doing it in the right way.
      data payload will be received by your application and you have to parse it as json.

  2. notificationRequestModel.setTo(“eHp6V2Wtr4I:APA91bFLHRuScumQB0lnNRmeirxu5kV2lUPDZ8SbZKz”);
    setTo ? what…
    Can you please tell me what is the eHp6V2Wtr4I:APA91bFLHRuScumQB0lnNRmeirxu5kV2lUPDZ8SbZKz

    Thank you

Leave a Reply

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