All Projects → bytefish → Fcmjava

bytefish / Fcmjava

Licence: mit
FcmJava is a Java library for working with the Firebase Cloud Messaging (FCM) API.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Fcmjava

Laravel Firebase
A Laravel package for the Firebase PHP Admin SDK
Stars: ✭ 369 (+310%)
Mutual labels:  firebase-cloud-messaging
Wanderlust The Travellers App
👟 An android application for travellers which allows them to save their journey experiences at one place in an organizable way. For detailed description, read the README.md file. Moreover, the application's design follows the latest HCI and UI/UX design guidelines.
Stars: ✭ 23 (-74.44%)
Mutual labels:  firebase-cloud-messaging
Kotlin Firebase Group Chat
Group and OneonOne chat using firebase built in Kotlin similar to whatsapp.
Stars: ✭ 44 (-51.11%)
Mutual labels:  firebase-cloud-messaging
Fcm Django
FCM Django: Send push notifications via django to websites, iOS & android mobile devices through FCM (Firebase Cloud Messaging)
Stars: ✭ 495 (+450%)
Mutual labels:  firebase-cloud-messaging
Pyfcm
Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web)
Stars: ✭ 674 (+648.89%)
Mutual labels:  firebase-cloud-messaging
Reactivefirebasepushmessaging
Reactive implementation of Firebase notifications in Android with kotlin
Stars: ✭ 10 (-88.89%)
Mutual labels:  firebase-cloud-messaging
Mechahamster
Mecha Hamster is a game where you roll through customizable environments that you can share with your friends.
Stars: ✭ 314 (+248.89%)
Mutual labels:  firebase-cloud-messaging
Ionic Pwa
🚀 Build a Progressive Web App with Ionic and Angular. Push Notifications. Deployed to Firebase Hosting. The Complete guide to build your PWA. Service Workers. Lighthouse. Web Manifest
Stars: ✭ 87 (-3.33%)
Mutual labels:  firebase-cloud-messaging
Thenetwork Open
TheNetwork is a blog cum chat app. It's completely built using firebase. Users can post, comment, like and bookmark the blogs, also users can send follow requests to connect with people. Users can create events and also prepare an event roadmap. Pagination for realtime data is also included in chats, blogs and events.
Stars: ✭ 17 (-81.11%)
Mutual labels:  firebase-cloud-messaging
Cocos2dx Cpp Sample
Firebase Cocos2d-x samples
Stars: ✭ 42 (-53.33%)
Mutual labels:  firebase-cloud-messaging
Rxfirebase
Rxjava 2.0 wrapper on Google's Android Firebase library.
Stars: ✭ 509 (+465.56%)
Mutual labels:  firebase-cloud-messaging
Applozic Android Sdk
Android Real Time Chat & Messaging SDK
Stars: ✭ 611 (+578.89%)
Mutual labels:  firebase-cloud-messaging
Pushclient
A cross-platform method of using Firebase Cloud Messaging (FCM) to receive push notifications
Stars: ✭ 33 (-63.33%)
Mutual labels:  firebase-cloud-messaging
Fcm
Ruby bindings to Firebase Cloud Messaging (FCM) for Android, iOS or Web
Stars: ✭ 409 (+354.44%)
Mutual labels:  firebase-cloud-messaging
Firebase Admin Node
Firebase Admin Node.js SDK
Stars: ✭ 1,050 (+1066.67%)
Mutual labels:  firebase-cloud-messaging
Firebase Js Sdk
Firebase Javascript SDK
Stars: ✭ 3,844 (+4171.11%)
Mutual labels:  firebase-cloud-messaging
Chatapp
Chat App with all functionality private chat, contacts, friends request, find friends,for profile settings image cropper functionality, settings, logout also send text, image and all type of files, delete your files for you and everyone , login with email and mobile number and real time database firebase and for notification purpose Node Js used.
Stars: ✭ 25 (-72.22%)
Mutual labels:  firebase-cloud-messaging
Node Gcm
A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
Stars: ✭ 1,286 (+1328.89%)
Mutual labels:  firebase-cloud-messaging
Pushraven
A simple Java library to interface with Firebase Cloud Messaging (FCM) API. Pushraven allows you to push notifications to clients in very few lines of code.
Stars: ✭ 67 (-25.56%)
Mutual labels:  firebase-cloud-messaging
Whatsup
**Deprecated** Real time chat app written in Swift 4 using Firebase and OTP Authentication
Stars: ✭ 39 (-56.67%)
Mutual labels:  firebase-cloud-messaging

FcmJava

On the Firebase Admin SDK for Java

This library was written at a time, when the official Firebase Admin SDK for Java did not support the Cloud Messaging API. The most recent release of the Firebase Admin SDK now also contains the Cloud Messaging, so the official SDK can be used instead of this library. You can find the official Firebase Admin SDK for Java at: https://firebase.google.com/docs/admin/setup.

Table of Contents

Description

FcmJava is a library for working with the Firebase Cloud Messaging (FCM) API.

Maven Dependencies

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

<dependency>
  <groupId>de.bytefish.fcmjava</groupId>
  <artifactId>fcmjava-core</artifactId>
  <version>2.5</version>
</dependency>

<dependency>
  <groupId>de.bytefish.fcmjava</groupId>
  <artifactId>fcmjava-client</artifactId>
  <version>2.5</version>
</dependency>

Quickstart

The Quickstart shows you how to work with FcmJava.

FcmClient

// 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;
        }

        @JsonProperty("firstName")
        public String getFirstName() {
            return firstName;
        }

        @JsonProperty("lastName")
        public String getLastName() {
            return lastName;
        }
    }

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

        // Create the Client using system-properties-based settings:
        try (FcmClient client = new FcmClient(PropertiesBasedSettings.createFromDefault())) {

            // Message Options:
            FcmMessageOptions options = FcmMessageOptions.builder()
                    .setTimeToLive(Duration.ofHours(1))
                    .build();

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

            // Assert Results:
            Assert.assertNotNull(response);

            // Make sure there are no errors:
            Assert.assertNotNull(response.getMessageId());
            Assert.assertNull(response.getErrorCode());
        }
    }
}

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 fcmjava.properties to ensure the API Key secret does not reside in code or leaks into the public. The default location of the fcmjava.properties is System.getProperty("user.home") + "/.fcmjava/fcmjava.properties".

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

fcm.api.url=https://fcm.googleapis.com/fcm/send
fcm.api.key=<YOUR_API_KEY_HERE>

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/fcmjava.properties" 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;
    }

    @Override
    public String getFcmUrl() {
        return Constants.FCM_URL;
    }

    @Override
    public String getApiKey() {
        return apiKey;
    }
}

public class FcmClientSettingsTest {

    @Test
    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. So in order to configure a proxy for the HTTP requests, you can configure the HttpClientBuilder used in FcmJava. This is done by instantiating the DefaultHttpClient with your configured HttpClientBuilder.

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.apache.DefaultHttpClient;
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.HttpClientBuilder;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.junit.Test;

class FakeFcmClientSettings implements IFcmClientSettings {

    @Override
    public String getFcmUrl() {
        return "";
    }

    @Override
    public String getApiKey() {
        return "";
    }
}

public class HttpBuilderConfigurationTest {


    @Test
    public void testFcmClientWithProxySettings() {

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

        // 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"));

        // Create the Apache HttpClientBuilder:
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
                // Set the Proxy Address:
                .setProxy(new HttpHost("your_hostname", 1234))
                // Set the Authentication Strategy:
                .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
                // Set the Credentials Provider we built above:
                .setDefaultCredentialsProvider(basicCredentialsProvider);

        // Create the DefaultHttpClient:
        DefaultHttpClient httpClient = new DefaultHttpClient(settings, httpClientBuilder);

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

FAQ

How to interpret the FCM Response Messages

Issue #30 explains how to interpret the FCM response message and handle errors.

The user @yakuninv asks how to handle an errorneous FcmMessageResultItem in the FcmMessageResponse:

As part of error handling when sending NotificationMulticastMessage I need to map an erroneous FcmMessageResultItem to the token that caused an error. A typical use case is to remove not registered tokens from my Database.

Can I rely on the order of FcmMessageResultItems in the FcmMessageResponse? Does it correspond to the order of registrationIds provided in the constructor of the NotificationMulticastMessage?

The user @culebras has written a good summary:

Take a look into this thread, I think it will answer your question (looks like the order is the same):

  • So for the FcmMessageResultItems with errorCode equals to NotRegistered you will want to remove those tokens for your DB.
  • For the FcmMessageResultItems with errorCode equals to Unavailable maybe you will want to resend the message for those tokens.
  • For the FcmMessageResultItems with registration_id not null, you will want to update the tokens in your DB (I think that the new registration_id can be obtained in this library from FcmMessageResultItem.getCanonicalRegistrationId).
  • Etc.

But, regarding to the Canonical IDs, you have to consider this:

Because it looks like that:

In FCM, it seems the Canonical IDs are no longer used (or at the very least extremely rarely) because of how the Instance ID service works. To put it simply, the service works that there would only be one valid token per App Instance.

So the updating of tokens in your DB for those tokens which are refreshed in FCM cloud would be mostly done in the method onTokenRefresh() in your Android client application (in this method is where the device should send the token to your DB the first time the device register itself to the FCM and also when the token is refreshed in FCM).

So, it seems that is not very likely that you are going to receive FcmMessageResultItem with registration_id not null, but anyway, it is good idea to also expect this and update the canonical token of those FcmMessageResultItem.

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/com.google.firebase.quickstart.fcm D/MyFirebaseMsgService: From: /topics/news
09-17 21:10:45.251 10882-11300/com.google.firebase.quickstart.fcm D/MyFirebaseMsgService: Message data payload: {lastName=Wagner, firstName=Philipp}

Additional Resources

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].