Create Chat App for Android using GCM Part 1

Hello friends, in this post I will show you How to Create Chat App for Android using PHP, MySQL and GCM. I have already posted about GCM and Push Notifications in my earlier post as these are very important for creating our chat app. So in this post we will create a simple chat room where many users can interact with each other.

This post was very lengthy so I divided it into two parts. 

Part 1: This part will cover about creating server side codes and APIs.
Part 2: In this part we will actually build our chat application.

Create Chat App for Android Live Demo

You can also check this video demonstrating the final app that we will make in this tutorial.

You can also get a live demo of the app that we will be building from below. Just download the apk from the link given and install it to your device. But you need two device to check it.

Download APK

If you tested the apk and want to know how to code it then lets move ahead.

Prerequisite to Create Chat App for Android

It is highly recommended that you go through the above mentioned tutorial before moving ahead on this Create Chat App for Android Tutorial. So lets begin.

Create Chat App for Android Tutorial

In this post we will create server side APIs and in the next post we will built the android application. I am using WAMP server. You can also use other server’s basically what is needed is PHP and MySQL.  So lets start with creating database.

Creating Database

  • This is our database model.

database

  • So as you can see we have two tables one is users and other is messages. To create the above tables just copy the following SQL to your phpmyadmin.

  • Now we have our database. Time to code our API.

Creating RESTful API

  • I am using PHP Storm as I personally feel it is the best IDE. So whatever IDE you are using open it and create a new PHP Project in the WAMP’s root directory (c:/wamp/www). I have created SimplifiedCodingChat.
  • Now you have to create the following folder structure as shown in the below screenshot.

rest api

  • First download SLIM Framework 2.x and paste it inside your libs folder.
  • Now inside include folder create 3 php files Config.php, DbConnect.php and DbOperation.php.
  • First come inside Config.php and write the following code.

  • You may need to change the value of the first 4 constants according to your MySQL database. And you also need to put your Google API Key. If you don’t know what is the google api key please go through the Android Push Notification Tutorial using GCM first.
  • Now inside DbConnect.php write the following code. It is used to connect with the MySQL Database.

  • In DbOperation.php we will define functions for all the database related operations. Write the following code in DbOperation.php.

  • Inside libs folder create a new folder named gcm and inside it create a new file named gcm.php and write the following code. This file will handle our push notifications.

  • Now come inside v1 folder and create a .htaccess file and write the following.

  • Finally in v1 folder create index.php this file will handle all the API calls.

  • Thats it for the API Coding. Now you need a REST Client to Test the API. You can use Postman for google chrome.

Testing the API

  • You can use Postman for chrome or any other REST Client to test your API. The final API we built is shown in the following table

Base URL: http://localhost/SimplifiedCodingChat/v1/

URL Method Parameters
register POST name,email
send POST id, message
storegcmtoken/:id PUT token
messages GET
  • If you are having some problems or troubles you can get my php project for this Android Chat App for Android Tutorial from the link given below (You need to unlock the link).

So thats all for this part friends. You can follow the next part from the link given.

Create Chat Application in Android using GCM Part 2

In this part we will actually Create Chat App for Android using Android Studio. And yes feel free to ask by comments if having any troubles. Thank You 🙂