fcm java client

firebase fcm java client for serverside

License

License

GroupId

GroupId

org.riversun
ArtifactId

ArtifactId

fcm
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

fcm java client
firebase fcm java client for serverside
Project URL

Project URL

https://github.com/riversun/java-firebase-fcm-client
Source Code Management

Source Code Management

https://github.com/riversun/java-firebase-fcm-client

Download fcm

How to add to project

<!-- https://jarcasting.com/artifacts/org.riversun/fcm/ -->
<dependency>
    <groupId>org.riversun</groupId>
    <artifactId>fcm</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.riversun/fcm/
implementation 'org.riversun:fcm:0.2.0'
// https://jarcasting.com/artifacts/org.riversun/fcm/
implementation ("org.riversun:fcm:0.2.0")
'org.riversun:fcm:jar:0.2.0'
<dependency org="org.riversun" name="fcm" rev="0.2.0">
  <artifact name="fcm" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.riversun', module='fcm', version='0.2.0')
)
libraryDependencies += "org.riversun" % "fcm" % "0.2.0"
[org.riversun/fcm "0.2.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.json : json jar 20160810

test (1)

Group / Artifact Type Version
junit : junit jar 4.7

Project Modules

There are no modules declared in this project.

Overview

java-firebase-fcm-client is push notification client for firebase cloud messaging API.

  • You can use on your server to send downstream message from firebase.
  • Whether the mobile(like Android) application is foreground or background, you can handle push notifications in the same way, without showing notification in the notification tray.

It is licensed under MIT license.

Quick start

Example Server->Mobile entity(mobile devices,browser front-end apps) (like Android)

  • Push notification to Android Device from your server
  • You can write like this code on your server.
package com.example;

import org.riversun.fcm.FcmClient;
import org.riversun.fcm.model.EntityMessage;
import org.riversun.fcm.model.FcmResponse;

public class SendMessageExample1 {
	public static void main(String[] args) {

		FcmClient client = new FcmClient();
		// You can get from firebase console.
		// "select your project>project settings>cloud messaging"
		client.setAPIKey("[YOUR_SERVER_API_KEY_HERE]");

		// Data model for sending messages to specific entity(mobile devices,browser front-end apps)s
		EntityMessage msg = new EntityMessage();

		// Set registration token that can be retrieved
		// from Android entity(mobile devices,browser front-end apps) when calling
		// FirebaseInstanceId.getInstance().getToken();
		msg.addRegistrationToken("REGISTRATION_TOKEN_FOR_AN_ANDROID_DEVICE_HERE");

		// Add key value pair into payload
		msg.putStringData("myKey1", "myValue1");
		msg.putStringData("myKey2", "myValue2");

		// push
		FcmResponse res = client.pushToEntities(msg);

		System.out.println(res);

	}
}

EntityMessage.java simply wraps following JSON.

{ "data":{
    "myKey1":"myValue1",
    "myKey2":"myValue2"
  },
  "registration_ids":["your_registration_token1","your_registration_token2]
}

Example Receive push notification on Android

Whether the application is foreground or background, you can handle push notifications in the same way.

Example for Android is here.

https://github.com/riversun/android-firebase-fcm-client

Example for Android overview

- MyFirebaseMessagingService.java

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Map<String, String> data = remoteMessage.getData();
        String value1 = data.get("myKey1");
        String value2 = data.get("myKey2");
    }
}

- MyFirebaseInstanceIDService.java

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        //registration token
        final String registrationToken = FirebaseInstanceId.getInstance().getToken();

        //TODO
        //Send to the server to register and identify the entity(mobile devices,browser front-end apps) in order to send a push notification to this entity(mobile devices,browser front-end apps).

    }
}

- Add services to your AndroidManifest.xml

<manifest>
  <application>
  ...

  <service android:name=".MyFirebaseMessagingService">
    <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
  </service>
  <service android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
  </service>
  ...
  </appliation>
</manifest>

- [project]/app/build.gradle

Add this into dependencies

 compile 'com.google.firebase:firebase-core:10.0.1'
 compile 'com.google.firebase:firebase-messaging:10.0.1'

Add this, it is important to place this at the bottom line.

apply plugin: 'com.google.gms.google-services'

- [project]/build.gradle

Add this into dependencies

 classpath 'com.google.gms:google-services:3.0.0'

if you want to check if google play service is available on the entity(mobile devices,browser front-end apps).Write like this.

        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);

        if (resultCode != ConnectionResult.SUCCESS) {
            if (resultCode == ConnectionResult.SUCCESS) {

            } else {
                apiAvailability.getErrorDialog(this, resultCode, 1).show();
            }
        }

Links

Firebase Console

https://console.firebase.google.com/

Format

https://firebase.google.com/docs/cloud-messaging/http-server-ref https://firebase.google.com/docs/cloud-messaging/send-message

Example code for Android (receiving message)

https://github.com/riversun/android-firebase-fcm-client

Downloads

maven

  • You can add dependencies to maven pom.xml file.
<dependency>
  <groupId>org.riversun</groupId>
  <artifactId>fcm</artifactId>
  <version>0.2.0</version>
</dependency>

Versions

Version
0.2.0
0.1.1
0.1.0