Firebase Cloud Messaging – XMPP Server example to receive Upstream Messages | Spring Integration

In the article Firebase Cloud Messaging – How to Send Upstream Messages, we have created an Android App that can send Upstream Messages. Today, we’re gonna look at way to create a Spring Boot Application Server that can receive those Upstream Messages with Spring Integration.

Related Posts:
Firebase Cloud Messaging – How to Send Upstream Messages | Android
How to start Spring Integration with Spring Boot

I. Project Overview

1. Architecture

fcm-xmpp-server-upstream-message-spring-integration-architecture

2. Send Upstream Messages

To know way to create an Android App Client that can send Upstream Messages, please read this post:
Firebase Cloud Messaging – How to Send Upstream Messages | Android

3. Receive Upstream Messages

Spring Integration provides XMPP adapters which support receiving XMPP chat messages:

3.1 Configure the XMPP namespace

– Include in the headers of your XML configuration file:

3.2 Establish XMPP connection

But to deal with Firebase Cloud Messaging, we must meet two important requirements:
– initiate a Transport Layer Security (TLS) connection.
– a SASL PLAIN authentication using Sender_Id@gcm.googleapis.com and Server key as the password.

For that complexity, we should use Spring JavaConfig-style of the XMPP Connection configuration:

3.3 Inbound Message Channel Adapter

payload-expression is used for the ChatMessageListeningEndpoint.
For the GCM protocol (to work with FCM), we extract the body using expression:

3.4 Handler for service-activator

Everytime an upstream message is received, get() method will be invoked with the help of GcmExtensionProvider for XMPP Extension.

3.5 XMPP Extension

With XMPP messaging extension is FCM, Smack provides the particular org.jivesoftware.smackx.gcm.provider.GcmExtensionProvider and registers that by default with the smack-experimental jar in the classpath.

To import that jar file, add dependency:

4. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.4.RELEASE
– Spring Boot: 2.0.0.M2

5. Structure

fcm-xmpp-server-upstream-message-spring-integration-structure

integration.xml includes header for Spring Integration and defines bean for Channel, Adapter and Service Activator.
AppConfiguration is @Configuration Component that defines XmppConnectionFactoryBean Bean for inbound-channel-adapter XMPP Connection in integration.xml.
FCMReceiver is handler class with get method for Service Activator.
– Dependencies for Spring Boot Web, Integration, XMPP, Smack in pom.xml

II. Practice

1. Get Sender Id and Server Key

Go to Settings of your Firebase Project in Firebase Console.

2. Create Spring Boot project

– Using Spring Tool Suite/Eclipse to create Spring Boot project.
– Add Dependencies to pom.xml file:

3. Define Beans for Channel, Adapter and Service Activator

4. Define Bean for XMPP Connection

The subscription-mode initiates the Roster listener to deal with incoming subscriptions from other users. FCM fully disables it. So we should configure it with:

5. Handler Class for Service Activator

6. Run & Check Result

– Config maven build:
clean install
– Run project with mode Spring Boot App.

If the console show the Exception:

It is just a Smack issue, don’t mind. It doesn’t block incoming messages. Everything work well after this.

– Send upstream Messages from Android App Client:
Open Demo App in Tutorial: Firebase Cloud Messaging – How to Send Upstream Messages | Android to send message:
firebase-cloud-messaging-upstream-messages-ui

– Result in Console Window:

III. Source Code

SpringBootXMPPServer


Related Posts


Got Something To Say:

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

*