Home » Android » Android background service for Smack (XMPP) chat application

Android background service for Smack (XMPP) chat application

Posted by: admin June 15, 2020 Leave a comment


I am implementing a chat app using asmack library. I want to start a always running service where all the connection management task is to be done and that should be keep on receiving the incoming packets and saving the messages in my sqlite database. And as my app is launched I want my app to be notified too for the incoming messages while the background service is inserting the messages into sqlite database.
How to implements such structure with sticky service. Is partial wake lock also needed. Since, images are also to be downloaded using http partially sometimes on specific demand.

How to&Answers:

You can start the service after registration on the server.

onStartCommand of service login the user.

Implement service with PacketListener,ConnectionListener,InvitationListener

PacketListener for Receiving Groupcaht or One to One chat messages.

ConnectionListener to check if connection to the server is connected or disconnected.

InvitationListener For getting Group invitations.

After successful login add these listeners.

Now when the message is received save that message in local database and send a Broadcast for the activities who want the message to be received.

Check if the app is in the background if yes then send the notification for the new message

Mail me if any buddy face any issue [email protected]