Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

<?php
 
   //Storing new user and returns user details
    
   function storeUser($name, $email, $gcm_regid, $userid=0) {
        // insert user into database
        $sql = "INSERT INTO gcm_users (gcm_regid, user_id, created_at) VALUES ('$gcm_regid', $userid, NOW())";
        $result = mysql_query($sql);
        // 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 {
                $result = mysql_query("UPDATE gcm_users SET user_id = $userid WHERE gcm_regid = '$gcm_regid'");
//            return false;
        }
    }
 
    /**
     * Get user by email
     */
   function getUserByEmail($email) {
        $result = mysql_query("SELECT * 
                                    FROM gcm_users 
                                    WHERE email = '$email'
                                    LIMIT 1");
        return $result;
    }
 
    // Getting all registered users
  function getAllUsers() {
        $result = mysql_query("select * 
                                    FROM gcm_users");
        return $result;
    }
 
    // Validate user
  function isUserExisted($email) {
        $result    = mysql_query("SELECT email 
                                       from gcm_users 
                                       WHERE email = '$email'");
                                        
        $NumOfRows = mysql_num_rows($result);
        if ($NumOfRows > 0) {
            // user existed
            return true;
        } else {
            // user not existed
            return false;
        }
    }
     
    //Sending Push Notification
   function send_push_notification($registatoin_ids, $message) {
        $msg = array(
                'message'       => $message['price'],
                'title'         => $message['price'],
                'subtitle'      => 'This is a subtitle',
                'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
                'vibrate'       => 1,
                'sound'         => 1,
                'largeIcon'     => 'large_icon',
                'smallIcon'     => 'small_icon'
        );        
 
        // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';
 
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $msg,
        );
        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );
        //print_r($headers);
        // 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;die;
    }