Sunday, 1 February 2015

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

In this tutorial i give the instruction from the previous post Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL Part - 1 if you are new to this please go through my previous post.
 Now lets start the coding from the PART - 1


5. Create another file named GCM.php This file used to send push notification requests to GCM server.
GCM.php
<?php
class GCM {
    //put your code here
    // constructor
    function __construct() {
         
    }
    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message) {
        // include config
        include_once './config.php';
        // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );
        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();
        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        // Close connection
        curl_close($ch);
        echo $result;
    }
}
?>
6. Create a new file called register.php This file receives requests from android device and stores the user in the database.
register.php
<?php
// response json
$json = array();
/**
 * Registering a user device
 * Store reg id in users table
 */
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["regId"])) {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $gcm_regid = $_POST["regId"]; // GCM Registration ID
    // Store user details in db
    include_once './db_functions.php';
    include_once './GCM.php';
    $db = new DB_Functions();
    $gcm = new GCM();
    $res = $db->storeUser($name, $email, $gcm_regid);
    $registatoin_ids = array($gcm_regid);
    $message = array("product" => "shirt");
    $result = $gcm->send_notification($registatoin_ids, $message);
    echo $result;
} else {
    // user details missing
}
?>
7. Create another file called send_message.php This file used to send pushnotification to android device by making a request to GCM server.
send_message.php
<?php
if (isset($_GET["regId"]) && isset($_GET["message"])) {
    $regId = $_GET["regId"];
    $message = $_GET["message"];
     
    include_once './GCM.php';
     
    $gcm = new GCM();
    $registatoin_ids = array($regId);
    $message = array("price" => $message);
    $result = $gcm->send_notification($registatoin_ids, $message);
    echo $result;
}
?>
In this post i conclude from this step i will continue this concept on later post...pls comment below...

No comments:

Post a Comment