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/

URLMethodParameters
registerPOSTname,email
sendPOSTid, message
storegcmtoken/:idPUTtoken
messagesGET
  • 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 🙂

54 thoughts on “Create Chat App for Android using GCM Part 1”

      • “Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.”

        deprecated may be an assertive word, but gcm is not recommended by google for new projects.

        Reply
      • deprecated may be an assertive word, but gcm is not recommended by google for new projects.

        “Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.”

        Reply
  1. i test this whole script after following all the procedures, uploaded it into server, i’ve changed all required credentials (Config.php to be specific), and run it using POSTMAN, and this is the result:

    Slim Application Error

    did i miss something?

    p/s:

    …it takes me about 5-6 times sending this post, everytime i submit my post it returns me to a 403 page, i think your site has something on that makes it so irresponsive…

    Reply
  2. .. who said that Google Cloud Messaging is deprecated? You might using other cloud messaging service, might be: xtify, urban airship, or pushy, but some people (mostly developers) say, (with “some people” with clause) that behind them is GCM .

    Reply
  3. .. who said that Google Cloud Messaging is deprecated? You might using other cloud messaging service, might be: xtify, urban airship, or pushy, but some people (mostly developers) say, (with “some people” with clause) that behind them is GCM . am telling you, the best of knowlegde , it is not.

    Reply
  4. “Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.”

    deprecated may be an assertive word, but gcm is not recommended by google for new projects.

    Reply
  5. deprecated may be an assertive word, but gcm is not recommended by google for new projects.

    “Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.”

    Reply
  6. Hello Belal Brother,Please Upload a chatting tutorial about a single chat like friend to friend chat using Firebase GCM,thanks in advance

    Reply
  7. Hi Sir,
    The Tutorial is best and works excellently on XAMPP.

    But when I upload the files to the online web server, the app malfunctions.

    I have changed the Config.php page like DBname, Host while hosting it to the online Web Server. I have also changed the path given in the app.

    I will thankful for your help!
    Thank You!

    Reply
      • Thank You, Sir for your reply.

        But I made all the changes in the configuration to the best of my knowledge. But the malfunction continues.

        Please can you list me the changes I should make for online server.

        Will thankful for your help!
        Thank You!

        Reply
        • i had the same experienced, the scripts are fully working fine on local server but some negative issue exists on putting all scripts on online server, and this i figure out: error exists not because of buggy script, it is because of capability of your main host or hosting i should say. You might using a shared server, am i right? take note that in shared server mysqlnd driver is not enabled, in summary, you need to use such or some mysqli prepared statements with object oriented programming using such VPS or Dedicated Server.Because those host services provide full root access and mysqlnd driver run by default.

          Reply
    • hi Shaun I also using web server but there is one problem i.e. getting 404 Page Not Found Error, if your app is running then help me.

      Reply
  8. Happy Ramadan belal.I have a questions you know the essential things required in the startup field , so why don”t you do it.Or you are already doing something.But above all thank you for these lovely tutorials.

    Reply
  9. Fatal error: Call to undefined method mysqli_stmt::get_result() in
    /home/test/public_html/test/chatting/include/DbOperation.php on line
    41

    Reply
    • i had the same experienced, the scripts are fully working fine on local server but some negative issue exists on putting all scripts on online server, and this i figure out: error exists not because of buggy script, it is because of capability of your main host or hosting i should say. You might using a shared server, am i right? take note that in shared server mysqlnd driver is not enabled, in summary, you need to use such or some mysqli prepared statements with object oriented programming using such VPS or Dedicated Server.Because those host services provide full root access and mysqlnd driver run by default.

      Reply
  10. why i received the message “Parse error: syntax error, unexpected ‘x’ (T_STRING) in C:\wamp\www\gcm_chat1\v1\index.php on line 6”, I don’t know what happen because I followed your example exactly, please help me to solve the problem, because I quite new in android

    Reply
  11. There is a mistake that needs a fix. DbOperation.php line 21
    Before: $stmt = $this->conn->prepare(“INSERT INTO users(name, email) values(?, ?)”);

    When I tried to register it gave me an error but

    After:

    $stmt = $this->conn->prepare(“INSERT INTO users(name, email,gcmtoken) values(?, ?,’A’)”);
    It worked for me by inserting gcmtoken.

    Reply
  12. Thanks you are really good in explaining this.
    Can this be uploaded to lets say Google app engine instead of a local server?
    What changes is needed for that?

    Reply
  13. Hi,

    I get an error ” java.lang.NullPointerException: Attempt to invoke virtual method ‘boolean .helper.AppController.isLoggedIn()” and the app crashes.

    Can u please help??

    Reply
  14. Please, help!
    Error: ————
    404 Page Not Found
    The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.

    Visit the Home Page
    ———-
    Database and table is created
    Installed Denwer

    Reply
  15. when i am enter name and enter email but it still load progress bar but do not display next act.so pls give me fast solution as soon as possible

    Reply
  16. hello sir ,
    when i have insert name and the email id in the field it will inserted in the database but couldn’t create the gcm tocken in “users” table

    Reply
  17. hello when i run this php code it show me error Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log. …this error how i solve this please please help me and reply soon as possible..please help me belal

    Reply
  18. Hello guys, I implemented the code of this tutorial ( and changed some pieces of it regarding to the needs of my app).
    But I have a problem. When I try to use $gcm->sendMessage(…), I got an error as a result, saying : Error 411 (Length Required)!!, and That’s an error. POST requests require a Content-length header. That’s all we know.
    Hope you guys can help me

    Reply
  19. i keep getting 404 not found on postman and browser .i don’t know what’s the problem, i already enabled Apache rewrite module.
    please help.

    Reply

Leave a Comment