Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
 
3
   //Storing new user and returns user details
4
 
5
   function storeUser($name, $email, $gcm_regid, $userid=0) {
6
        // insert user into database
7
	$sql = "INSERT INTO gcm_users (gcm_regid, user_id, created_at) VALUES ('$gcm_regid', $userid, NOW())";
8
        $result = mysql_query($sql);
9
        // check for successful store
10
        if ($result) {
11
 
12
            // get user details
13
            $id = mysql_insert_id(); // last inserted id
14
            $result = mysql_query(
15
                               "SELECT * 
16
                                     FROM gcm_users 
17
                                     WHERE id = $id") or die(mysql_error());
18
            // return user details 
19
            if (mysql_num_rows($result) > 0) { 
20
                return mysql_fetch_array($result);
21
            } else {
22
                return false;
23
            }
24
 
25
        } else {
26
		$result = mysql_query("UPDATE gcm_users SET user_id = $userid WHERE gcm_regid = '$gcm_regid'");
27
//            return false;
28
        }
29
    }
30
 
31
    /**
32
     * Get user by email
33
     */
34
   function getUserByEmail($email) {
35
        $result = mysql_query("SELECT * 
36
                                    FROM gcm_users 
37
                                    WHERE email = '$email'
38
                                    LIMIT 1");
39
        return $result;
40
    }
41
 
42
    // Getting all registered users
43
  function getAllUsers() {
44
        $result = mysql_query("select * 
45
                                    FROM gcm_users");
46
        return $result;
47
    }
48
 
49
    // Validate user
50
  function isUserExisted($email) {
51
        $result    = mysql_query("SELECT email 
52
                                       from gcm_users 
53
                                       WHERE email = '$email'");
54
 
55
        $NumOfRows = mysql_num_rows($result);
56
        if ($NumOfRows > 0) {
57
            // user existed
58
            return true;
59
        } else {
60
            // user not existed
61
            return false;
62
        }
63
    }
64
 
65
    //Sending Push Notification
66
   function send_push_notification($registatoin_ids, $message) {
67
	$msg = array(
68
		'message' 	=> $message['price'],
69
		'title'		=> $message['price'],
70
		'subtitle'	=> 'This is a subtitle',
71
		'tickerText'	=> 'Ticker text here...Ticker text here...Ticker text here',
72
		'vibrate'	=> 1,
73
		'sound'		=> 1,
74
		'largeIcon'	=> 'large_icon',
75
		'smallIcon'	=> 'small_icon'
76
	);        
77
 
78
        // Set POST variables
79
        $url = 'https://android.googleapis.com/gcm/send';
80
 
81
        $fields = array(
82
            'registration_ids' => $registatoin_ids,
83
            'data' => $msg,
84
        );
85
        $headers = array(
86
            'Authorization: key=' . GOOGLE_API_KEY,
87
            'Content-Type: application/json'
88
        );
89
        //print_r($headers);
90
        // Open connection
91
        $ch = curl_init();
92
 
93
        // Set the url, number of POST vars, POST data
94
        curl_setopt($ch, CURLOPT_URL, $url);
95
 
96
        curl_setopt($ch, CURLOPT_POST, true);
97
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
98
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
99
 
100
        // Disabling SSL Certificate support temporarly
101
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
102
 
103
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
104
 
105
        // Execute post
106
        $result = curl_exec($ch);
107
        if ($result === FALSE) {
108
            die('Curl failed: ' . curl_error($ch));
109
        }
110
 
111
        // Close connection
112
        curl_close($ch);
113
        echo $result;die;
114
    }