A Simple Chat Application in Android Using GCM and Server Side – PHP.

By | September 23, 2017

Hi all,

In Today’s Tutorial I will help you to make a simple chat application in Android using Google Cloud Messaging. I am using PHP as server side script.

Here are the things you need to follow to make this application.

1. You need to register to Google for GCM.
2. You need a server where your script resides.[We will use our own system as Server in this application].

Please read this before starting with the application
After reading the above link, you will get a API key and Sender ID , which distinguishes your application.
So you will need it in the code, keep it somewhere safe.

You need to change the API Key inside Android source code in the sample Provided below.
Your API Key and SENDER ID will look something similar to this.

 
 String SENDER_ID = "243750827028";
 String API_KEY   = "AIzaSyCBI_c2izVrEPcJ509uZGVLdfWUAW-rg48";
	

Next Go through this link, till step 2 in that page because I have provided the source code below.

The usual flow of a simple GCM application is like this

1. User launches your application for the first time.
2. Your application will call “register” to register with GCM.
3. Google will send you back a “Registration ID” which will be unique for that device.
But beware these “Registration ID’s” will change when you update the application or reinstall it after uninstalling. So don’t use it to uniquely identify a user. A username/password combination or a Phone Number usually does the trick.

4. The device gets the “Registration ID” and store it locally, so that the device don’t need to register again each time the user run the
application.
5. Send this registration ID to your server and Store it there along with a unique identifier for the user. [Here we will use ‘username’ for the time being].
6. When the user taps send message button you need to call a server side script that will communicate with GCM server which will
send this message to corresponding device.
ie. User taps the button -> send the device_reg_id and message to your server -> send these parameters from your server to GCM
-> GCM send the message to Device identified by reg id.

So I am not going to paste the whole code here.
You can directly download it from below links

Client Complete Android source code.

PHP server side source code.

Here I have used a simple database structure for simplicity.
I am using username for identifying each user.

You need to make the same structure for the below sample to work.

GCM Sample DB structure

GCM Sample DB structure

For Server Side I am using XAMPP for Mac.
You can download the Server side source code and put it inside htdocs folder inside XAMPP/Applications.

Start your servers.

Please check the Screenshot below.

GCM Sample DB structure

Then Go to your browser and type “localhost”. if your server is running, then you will see “phpMyAdmin” Page.

For testing it in your system and in your device, Both should be in the same network, for example : both should be in the same WIFI netwrok of your home.
Please type “ifconfig” for Mac in terminal and check inside “en0” or “en1” to get your sytem IP. For Windows type “ipconfig”.

Before running the application change the “IP” in the Utils file.

I am not going to the Android source code because Complete source is already provided.

How to test?

1. Change the API Key and Sender ID in the application.

2. Make sure server side is set up. copy php code to your local server as described above.
Make sure you have sql tables set up.

3. Make sure your servers are running.

4. Your testing devices and Your server should be on the same network, otherwise you have to host an online server. You can use the Emulator also as one device, but it should be an emulator with “Google Play Services”. Android 5.0 is good to go.

5. Change the IP address in “Utils.java” to your system IP address.

Please leave your comments if you have problems understanding the above article.

9 thoughts on “A Simple Chat Application in Android Using GCM and Server Side – PHP.

  1. Pingback: Sync Adapter in Android – A Simple Example… | Free Tools Successful Bloggers Are Using

  2. Tshepo

    Thank you so much for this, worked perfectly. Do you have any idea on handling the push notification, like updating the respective chat activity after the message is received?

    Reply
  3. MK

    That really a nice and simple tutorial but using this tutorial I am always get Message: null and just able to send the text not able to receive from the other client can you help me with this

    Reply
  4. Pingback: Sync Adapter in Android – A Simple Example… | All Things Gadget

  5. gari

    i got a problem with the “android-support-v7-appcompat” in the RES>values>v21

    and also in the main app. There’s a red exclamation point which seems to be an error but i can find what really it is.

    please forgive me i just starting in android development.

    ill be waiting for the reply here.

    thanks.

    Reply

Leave a Reply

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