java – Chat App for Android using a XMPP Server and Google Cloud Messaging (or the newer Firebase Cloud Messaging) for Push Notifications-ThrowExceptions

Exception or error:

I’m developing a Chat App for Android. I have been reading for weeks about XMPP and Google Cloud Messaging (and Firebase Cloud Messaging) and I am still very confused.

Currently, I already setup an XMPP server locally (Ejabberd) and successfully connected my Android App to it using the Smack library.

To my understanding, I need to use GCM or the newer FCM for Push Notifications, so I already created a project in Google Cloud Platform. My Android App can connect to it using the Smack library too (instead of connecting to my XMPP server directly). In my server I have an small Java app that connects to GCM using the Smack library too.

Everything is fine until here. My big confusion is: How can I use my XMPP server with GCM for Push Notifications? Every documentation, examples, guides, tutorials I found for server-side implementations just tell me how to connect to GCM but none tell me how to use my XMPP server in conjunction with GCM. What I’m missing? My Java app just connects to GCM, receive and send messages from and to GCM, but my XMPP server is just sitting there doing nothing. Actually my Android App and server Java App use GCM exclusively, not my XMPP server.

I hope someone can help me to understand this as a whole, I am obviously missing some key elements to achieve this implementation.

How to solve:

You need to mix both Ejabberd and FCM together, that’s how all the big chat apps do it out there. For the very basics, there are 3 components: App Server connected via XMPP to FCM, Ejabberd and your client app.

  1. When the app is in the foreground, you use Smack library to connect directly to your Ejabberd server, send messages, change user’s presence, etc. The connection to your Ejabberd is kept during that time. You don’t send upstream messages during this time!
  2. Once the user navigates away from your app, you close the connection. The user is now considered “Away” or “Offline”.
  3. From this point and on, your App Server communicates with FCM to send downstream messages to the device using Smack library as well.
  4. On the client device: You handle the incoming message and show a notification. With Android N, users can reply directly from the notification. I assume that in this case, you would use FCM to send an upstream message to your app server since during that time, there’s no active connection to your Ejabberd server.
  5. Once the user taps on the notification, the app comes back to foreground and you re-connect to Ejabberd and back to step 1.

This is the very basic description of the architecture you should have to achieve what you want.

###

This is a sample java project to showcase the Firebase Cloud Messaging (FCM) XMPP Connection Server. This project is a very simple standalone server that I developed as a base of a larger project. It is an application server that we must implement in our environment. This server sends data to a client app via the FCM CCS Server using the XMPP protocol.

https://github.com/carlosCharz/fcmxmppserver

And also I’ve created a video in youtube where I explain what it does.

https://www.youtube.com/watch?v=PA91bVq5sHw

Hope you find it useful.

###

You should set up a Java server which connects to FCM (formerly GCM). Then you can from your device send an upstream message to FCM who then sends that upstream message to your java server and then within that Java server you can handle that upstream message to send a downstream message to the targeted device(s). Then on the device side you can handle those downstream messages being received to give a push notification.

A few useful links:

How to send an upstream message:

https://firebase.google.com/docs/cloud-messaging/upstream#sample-send

How to receive and handle downstream messages:

https://firebase.google.com/docs/cloud-messaging/downstream#sample-receive

How I set up an example Java server:

https://stackoverflow.com/a/38170310/4433653

###

I am also building chat application in Android using Smack v.4.1.8 which latest version of Smack, and our server is using ejabberd. I am not use any third party like GCM or Firebase to push downstream message from user to user message. If i am not wrong you going to use GCM or Firebase to push user to user message, if yes just dont do that.

Why?
TCP protocol is keep listening a invoke listener which you registered in App when the connecting establish and still connected. Smack has a listener which called “addAsyncStanzaListener()”

addAsyncStanzaListener is used to listen a receive packet in App. They have

 public void processPacket(Stanza packet) 

You can invoke that, and you will listen the packet over the time.

I do a research about keeping stable connection of Smack. Most of Android Smartphone have each configuration, and restriction. We cannot test all of devices.
Here my tips to keep connection stable:

  1. Use ForegroundService instead BackgroundService, i propose this approach because most of Android Device have restriction to an app which run in background. They will kill app when the app swipe from task manager. (e.g Asus zenphone have power management)
  2. ForegroundService will prevent app from being idle.
  3. Register ReconnectingManager
  4. Register Pingfailed
  5. Register ServerPingWithAlarmManager

And that’s it. Any enquiries, just comment below 🙂

Best Regard
R Aditya Gumay

###

Your suggestion is valid for a connection with tcp/ip never drop. which is not the case in Mobile 3G/4G network. So in real 3G/4G network, the mobile client/APP might be in zombie state(disconnected).
That is why you need Firebase/FCM to come to play.

Leave a Reply

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