13

I am developing an Android app in which I have to implement chat messaging. I would like one to one chat or a group chat.

But I have no idea how to start. Please help me with this stuff. Any help will be appreciated.

ErikE
  • 48,881
  • 23
  • 151
  • 196

2 Answers2

32

A simple chat mechanism will have 2 basic functionalities

  1. Send the message to server (with info about the recipient)

  2. Receive the message from server (designated for my user name)

First step is simple, we can create a web service which will accept the message with additional information about recipient(s). We can create it using any server side language.

Step 2, that is fetching the message from server can be done using 2 techniques, Pull the message (using polling) from server, or Push the message from server to android phone

  1. Polling: In this, the android device will keep accessing server after a few seconds to check if there is a message available for user. This again can be implemented using a simple async task at the client side which will keep calling a web service after say 2-3 seconds. This is fine to use if we are planning to enable chatting only when user is accessing the app (no notifications like gmail or facebook), so that we can kill the polling service when not in use (otherwise it will eat up resources).

  2. Push notifications: a better option is to use push notifications. Android provide Google cloud messaging or GCM (http://developer.android.com/google/gcm/index.html) which will help achieve push from server easily. Otherwise you can try a third party API like urbanairship or pushwoosh depending on your requirement. Push notifications will help the user to receive messages even when he is not using the app.

So in nutshell, a webservice to receive the messages and a push notification mechanism should be sufficient to implement a chat service in android.

Little bit about UrbanAirship

I used UA in one of my projects for push notifications as I needed to support both iOS and Android. If you just want to support Android GCM might also be a good option.

Coming back to UA, check this for sample code and usage: https://docs.urbanairship.com/display/DOCS/Home

The way it works is simple, when someone installs the app and is connected to internet, app registers itself to the UA service. A unique code is specified for each installed app (this is the time when you can capture the user name and unique code and store somewhere in your DB). Next UA provides an API using which you can push a message to designated recipient(s), using the unique codes which are available with UA. These messages can be received by android app and used as per the requirement. Even if the app is not running we can show a notification just like when we receive an email or a message

Kamal
  • 5,462
  • 8
  • 45
  • 58
  • Very well explained Kamal .. Can you elaborate your step 2 with some code or links –  Jan 07 '13 at 06:00
  • Thnks Kamal.. Can you put some light on push messages means how to implement urbanship .. –  Jan 07 '13 at 06:01
  • 1
    Hi what you explained is really helpful Kamal . Just have one doubt I hope you can solve it by answering. As per I know UA sends push notification for all the devices in which that app is installed. But in one to one chat push notification should only be sent to only one particular user mentioned. How to achieve this. If anyone can answer my question it will really be helpful to me. Thanks in advance – newBie Oct 22 '13 at 07:56
  • @Kamal Im using FCM for notifications when the app is not in use, but what if he is in a One to One conversation with some one or a group chat is going on? Is it preferable to go along with FCM notification only, or should I implement anything else for that? A huge number of conversation might happen that way. Please suggest.. – Ari Oct 27 '16 at 10:53
0

You can use an existing platform like Scringo. It gives you a one-on-one chat as well as group chat (both the client and the server) as well as the push notification service.

hungary54
  • 767
  • 8
  • 11