Sunday, 25 January 2015

Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL Part - 1

"Google Cloud Messaging for Android (GCM) is a service that helps developers send data from servers to their Android applications on Android devicesโ€ By using this service you can sent data to your client application from your Remote database or server that you have. using this service you can sent new data whenever your application need that.Integrating GCM in your android application enhances user experience and saves lot of battery power.


Overview of Google Cloud Messaging:





Registering Google Cloud Messaging:

1. Goto  Google APIs Console page and create a new project. (If you havenโ€™t created already otherwise it will take you to dashboard).

2. After creating your project you can see the project id in the url. Note the project id it will be used as SENDER ID in android your project. (Example: in #project:4608669299 after semicolon 4608669299 is the sender id)
3. After that click on Services on the left panel and turn on Google Cloud Messaging for Android.
4. If you are done, click on API Access and note the API Key. This API key will be used when sending requests to GCM server.

Create MySQL Database For your Application:

1. Open phpmyadmin panel by going to http://localhost/phpmyadmin and create a database called gcm. (if your localhost is running on port number add port number to url)
2. After creating the database, select the database and execute following query in SQL tab to creategcm_users table.
CREATE TABLE IF NOT EXISTS `gcm_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gcm_regid` text,
  `name` varchar(50) NOT NULL,
  `email` varchar(255) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
3. Create another file called db_connect.php This file handles database connections, mainly opens and closes connection.
db_connect.php
<?php
  
class DB_Connect {
  
    // constructor
    function __construct() {
  
    }
  
    // destructor
    function __destruct() {
        // $this->close();
    }
  
    // Connecting to database
    public function connect() {
        require_once 'config.php';
        // connecting to mysql
        $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        // selecting database
        mysql_select_db(DB_DATABASE);
  
        // return database handler
        return $con;
    }
  
    // Closing database connection
    public function close() {
        mysql_close();
    }
  
}
?>
4. Create a new file named db_functions.php This file contains function to perform database CRUD operations. But i wrote function for creating user only.
db_functions.php
<?php
class DB_Functions {
    private $db;
    //put your code here
    // constructor
    function __construct() {
        include_once './db_connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }
    // destructor
    function __destruct() {
         
    }
    /**
     * Storing new user
     * returns user details
     */
    public function storeUser($name, $email, $gcm_regid) {
        // insert user into database
        $result = mysql_query("INSERT INTO gcm_users(name, email, gcm_regid, created_at) VALUES('$name', '$email', '$gcm_regid', NOW())");
        // check for successful store
        if ($result) {
            // get user details
            $id = mysql_insert_id(); // last inserted id
            $result = mysql_query("SELECT * FROM gcm_users WHERE id = $id") or die(mysql_error());
            // return user details
            if (mysql_num_rows($result) > 0) {
                return mysql_fetch_array($result);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    /**
     * Getting all users
     */
    public function getAllUsers() {
        $result = mysql_query("select * FROM gcm_users");
        return $result;
    }
}
?>

No comments:

Post a Comment