Firebase Cloud Messaging (FCM) with Java

Some time ago I needed a simple way to send Push Messages with Firebase Cloud Messaging (FCM), so I wrote FcmJava:

FcmJava implements the entire Firebase Cloud Messaging HTTP Protocol and supports:

  • Downstream HTTP Messages
  • Notification Payloads
  • Topic Messages
  • Device Group Messages

Firebase Cloud Messaging (FCM) is basically the successor to Google Cloud Messaging, so it was easy to port my .NET library to Java.

Maven Dependencies

You can add the following dependencies to your pom.xml to include FcmJava in your project.




The Quickstart shows you how to work with FcmJava and with Firebase Cloud Messaging for an Android application.

The Android application is taken from Googles GitHub repository for the Firebase Quickstart with Android:

The example server application uses FcmJava to send a Push data message to the news Topic. I have implemented the FileContentBasedSettings class to read the API token from a file.


// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package de.bytefish.fcmjava.integration;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.bytefish.fcmjava.client.FcmClient;
import de.bytefish.fcmjava.constants.Constants;
import de.bytefish.fcmjava.http.options.IFcmClientSettings;
import de.bytefish.fcmjava.model.options.FcmMessageOptions;
import de.bytefish.fcmjava.model.topics.Topic;
import de.bytefish.fcmjava.requests.topic.TopicUnicastMessage;
import org.junit.Ignore;
import org.junit.Test;

import java.nio.charset.Charset;
import java.time.Duration;

public class FcmClientIntegrationTest {

    private class PersonData {

        private final String firstName;
        private final String lastName;

        public PersonData(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;

        public String getFirstName() {
            return firstName;

        public String getLastName() {
            return lastName;

    @Ignore("This is an Integration Test using system properties to contact the FCM Server")
    public void SendTopicMessageTest() throws Exception {

        // Creates the Client using the default settings location, which is System.getProperty("user.home") + "/.fcmjava/":
        FcmClient client = new FcmClient();

        // Message Options:
        FcmMessageOptions options = FcmMessageOptions.builder()

        // Send a Message:
        client.send(new TopicUnicastMessage(options, new Topic("news"), new PersonData("Philipp", "Wagner")));

FcmClientSettings and API Key

The FcmClient can be instantiated with IFcmClientSettings to supply the API Key. By default the FcmClient uses the PropertiesBasedSettings, which locate the settings in a default location. If you need to supply the API Key in a different way, you can simply instantiate the FcmClient with a custom IFcmClientSettings implementation.

Using the PropertiesBasedSettings

By default the FCM API Key is read from an external .properties file called to ensure the API Key secret does not reside in code or leaks into the public. The default location of the is System.getProperty("user.home") + "/.fcmjava/".

The file has to contain the FCM API Endpoint and the API Key:


If the properties are available in the default location you can simply instantiate the FcmClientas seen in the example.

You can use the PropertiesBasedSettings class to read the Properties and pass them into the FcmClient, if the Properties path differs from the default path:

  1. PropertiesBasedSettings.createFromDefault()
    • Uses the default file location of System.getProperty("user.home") + "/.fcmjava/" to read the properties. This is the recommended way of reading your API Key.
  2. PropertiesBasedSettings.createFromFile(Path path, Charset charset)
    • Uses a custom file location to read the settings from.
  3. PropertiesBasedSettings.createFromSystemProperties()
    • Uses the System Properties to initialize the settings.
  4. PropertiesBasedSettings.createFromProperties(Properties properties)
    • Uses the supplied Properties to build the FcmSettings.

Implementing the IFcmClientSettings interface

It's not neccessary to use the PropertiesBasedSettings for supplying an API Key to the FcmClient. You can easily implement the IFcmClientSettings interface and pass it into the FcmClient.

The following test shows a simple IFcmClientSettings implementation, that will be instantiated with the given API Key. Again I strongly suggest to not hardcode the Firebase Cloud Messaging API Key in code. This makes it possible to accidentally leak your credentials into public.

// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package de.bytefish.fcmjava.client.tests.settings;

import de.bytefish.fcmjava.client.FcmClient;
import de.bytefish.fcmjava.constants.Constants;
import de.bytefish.fcmjava.http.client.IFcmClient;
import de.bytefish.fcmjava.http.options.IFcmClientSettings;
import org.junit.Test;

class FixedFcmClientSettings implements IFcmClientSettings {

    private final String apiKey;

    public FixedFcmClientSettings(String apiKey) {
        this.apiKey = apiKey;

    public String getFcmUrl() {
        return Constants.FCM_URL;

    public String getApiKey() {
        return apiKey;

public class FcmClientSettingsTest {

    public void testFixedClientSettings() {

        // Construct the FCM Client Settings with your API Key:
        IFcmClientSettings clientSettings = new FixedFcmClientSettings("your_api_key_here");

        // Instantiate the FcmClient with the API Key:
        IFcmClient client = new FcmClient(clientSettings);


Configuring a Proxy

FcmJava uses Apache HttpClient for making requests to the Firebase Cloud Messaging server.

In order to configure a proxy for the HTTP requests, you can configure the HttpClientBuilder used in FcmJava. This is done by instantiating the HttpClient with your settings and then calling the configure method on it.

The following test shows how to build the FcmClient with a custom HttpClient, which configures a Proxy for the HttpClientBuilder.

// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package de.bytefish.fcmjava.client.tests;

import de.bytefish.fcmjava.client.FcmClient;
import de.bytefish.fcmjava.client.http.HttpClient;
import de.bytefish.fcmjava.http.client.IFcmClient;
import de.bytefish.fcmjava.http.options.IFcmClientSettings;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.junit.Test;

class MockFcmClientSettings implements IFcmClientSettings {

    public String getFcmUrl() {
        return "fcm_url";

    public String getApiKey() {
        return "your_api_key";

public class HttpBuilderConfigurationTest {

    public void testFcmClientWithProxySettings() {

        // Create Settings:
        IFcmClientSettings settings = new MockFcmClientSettings();

        // Create the HttpClient:
        HttpClient httpClient = new HttpClient(settings);

        // And configure the HttpClient:
        httpClient.configure((httpClientBuilder -> {

            // Define the Credentials to be used:
            BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();

            // Set the Credentials (any auth scope used):
            basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("your_username", "your_password"));

            // Now configure the HttpClientBuilder:
                    // Set the Proxy Address:
                    .setProxy(new HttpHost("your_hostname", 1234))
                    // Set the Authentication Strategy:
                    .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
                    // Set the Credentials Provider we built above:

        // Finally build the FcmClient:
        IFcmClient client = new FcmClient(settings, httpClient);

Android Client

I have decided to clone the messaging quickstart sample of Google, which is available at:

Now first subscribe to the news topic, then execute the above FcmJava application.

The Android app will now receive a message with the sent data included:

09-17 21:10:45.250 10882-11300/ D/MyFirebaseMsgService: From: /topics/news
09-17 21:10:45.251 10882-11300/ D/MyFirebaseMsgService: Message data payload: {lastName=Wagner, firstName=Philipp}

Additional Resources

I have written an example Weather Warning Application using FcmJava.

The Android Application can be found at:

The server-side using FcmJava can be found at:

How to contribute

One of the easiest ways to contribute is to participate in discussions. You can also contribute by submitting pull requests.

General feedback and discussions?

Do you have questions or feedback on this article? Please create an issue on the GitHub issue tracker.

Something is wrong or missing?

There may be something wrong or missing in this article. If you want to help fixing it, then please make a Pull Request to this file on GitHub.