How to Build an Android Group Chat Application

We’ve recently published a newer, up-to-date Android group chat tutorial using our new v4 SDK, and recommend following that tutorial.

In this blog post, we’ll walk you through how to build an Android group chat application with a number of different core chat features. We’ll cover how to build:

  • Basic messaging for multi-user group chat
  • Presence: ie. the ‘buddy list’ (live online/offline status of connected users)
  • History: stores and retrieves messages
  • Push notifications

In the end, you’ll have something that looks like the sample image below. Check out the full open source Android group chat GitHub repository here.


Project Details

Our Android group chat app allows a user to create a login identity that is then stored inSharedPreferences until logout. The initial chat room is titled MainChat, but a user can change chat rooms at any time.

An integer in the menu bar indicates how many users are in the current chat room. If you click that here now number, a modal with all users in the room is displayed. Clicking on a user in the herenow modal will display the login time of the corresponding user.

Clicking on a message in the chat room will send a poke to that user, notifying them that you are in the chat room.

Chat Functionality and Features

Core Chat Messaging

Publish/Subscribe Messaging powers the core messaging of the Android group chat app, enabling users to send and receive messages in realtime.

When a user sends a message, the code creates a ChatMessage object that holds the username, message text, and timestamp for the given message. ChatMessages are then serialized into a JSONObject and published to all users on the current channel via PubNub.

Next, a message is received using PubNub’s subscribe method. The JSONObject is deserialized back into a ChatMessage and added to a list of messages from the current chat room.


Realtime User Online/Offline List

Presence powers our ‘buddy list’, a realtime view of who is online, who is offline, and live-updates based on state change.

If a user is online, a green dot is displayed next to their name. Join and Leave events are updated in realtime as well, so when a user logs off, you are notified immediately.


Presence updates the number of users currently in a room, but when you want to see the users currently in the chat, hereNow is used. A modal opens and is populated with the users currently subscribed to the chat room. Clicking on a user will tell you their login time, and the modal can be dismissed by clicking done.

Historic Messages

To store and retrieve historic chat messages, Storage & Playback is used. When a user opens the Android group chat app and is sent to MainChat, or when a user changes rooms, a call to PubNub’s history function loads the last 100 messages from the current channel so you know what other users have been talking about when you join.

Last Login


The PubNub State API allows you to define state data on the current channel. You can attach an object to your UUID that is accessible to anyone on the channel. This chat app uses the state API to attach login time to all online users’ UUIDs. When a user in the here now modal is clicked, their login time is read from the state of the channel and displayed in a Toast message.

Push Notifications


Enable push notifications on a channel and attach your GCM device ID to that channel and you are all set to receive pushes. When you tap a user’s message in a chat room, a PubNub push notification is sent through GCM protocols to notify the user that you are waiting for them.

If you want to read more about getting started with GCM for Android push notifications, we have a full tutorial.

Make It Your Own!

Now that we’ve covered the basics, we encourage you to extend this tutorial and make it your own! For example, you can incorporate chat room features, perhaps using hereNow with an empty channel parameter to show users all active chat rooms and their occupancies.

To do this, you’ll need your own publish/subscribe keys. To get these, sign up for a PubNub account, it’s quick and easy! You can find your unique PubNub keys in the PubNub Admin Dashboard. Then, enable and customize the Presence, Storage & Playback, and Push Notification features. Once you have done this, simply replace the String values for PUBLISH_KEYand SUBSCRIBE_KEY in me.kevingleason.pubnubchat.Constants.

To enable Push Notifications, you will need to register your app for GCM. You will then receive an API Key which you can use to enable Push Notifications in the PubNub Admin Dashboard, as well as a Sender ID which you should use to replace the GCM_SENDER_ID in theme.kevingleason.pubnubchat.Constants file.


And that’s it! We’ve walked through how to build a fully-featured group chat application for Android mobile devices. If you’re a fan of Android, we have a ton of other great realtime Android tutorials, as well as additional chat tutorials for JavaScript and iOS.

Try PubNub Today

Connect up to 100 devices for Free