2014-10-04 168 views
-1

我遇到GCM問題,無法向所有設備發送消息。GCM向所有設備發送消息

這不是原始碼,而是我自己的版本!

send_message.php:

<?php 
if (isset($_POST["regId"]) && isset($_POST["message"])) { 
//$regId = $_POST["regId"]; 
$message = $_POST["message"]; 

include './GCM.php'; 

$gcm = new GCM(); 

//$registatoin_ids = array($regId); 
$message = array("price" => $message); 


    include_once 'db_functions.php'; 

    $db = new DB_Functions(); 
    $users = $db->getAllUsers(); 

    if($users != false){ 
     $no_of_users = mysql_num_rows($users); 
    }else{ 
     $no_of_users = 0; 
    } 


    $i = 0; 

    while ($row = mysql_fetch_array($users)) { 
     $ids[$i] = $row["gcm_regid"]; 
     $i++; 

     foreach ($ids as $value){ 
      echo $value; 
      $registatoin_ids = $value; 
      json_encode($registatoin_ids); 
       $result = $gcm->send_notification($registatoin_ids, $message); 
       echo $result; 
    } 
    } 

echo $value; 
echo $registatoin_ids; 
} 
?> 

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; 
} 
} 
?> 

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; 
    } 
} 

/** 
* Get user by email and password 
*/ 
public function getUserByEmail($email) { 
    $result = mysql_query("SELECT * FROM gcm_users WHERE email = '$email' LIMIT 1"); 
    return $result; 
} 

/** 
* Getting all users 
*/ 
public function getAllUsers() { 
    $result = mysql_query("select * FROM gcm_users"); 
    return $result; 
} 

/** 
* Check user is existed or not 
*/ 
public function isUserExisted($email) { 
    $result = mysql_query("SELECT email from gcm_users WHERE email = '$email'"); 
    $no_of_rows = mysql_num_rows($result); 
    if ($no_of_rows > 0) { 
     // user existed 
     return true; 
    } else { 
     // user not existed 
     return false; 
    } 
} 

} 

?> 

的原廠l代碼來自Ravi Tamada。

回答

0

變化send_message.php到波紋管的代碼:

<?php 

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 


include_once 'db_functions.php'; 

if (isset($_GET["message"]) && isset($_GET["Number"])) { 



    $message = $_GET["message"]; 


     $db = new DB_Functions(); 



     $users = $db->getAllUsers(); 
     $regIds = array(); 

     while($row = mysql_fetch_array($users)) { 

     array_push($regIds,$row['gcm_regid']);  
     } 
     echo isset($users); 
     if ($users != false) 
      $no_of_users = mysql_num_rows($users); 
     else 
      $no_of_users = 0; 


    include_once './GCM.php'; 

    $gcm = new GCM(); 

    $message = array("message" => $message); 

    $result = $gcm->send_notification($regIds, $message); 

    echo $result; 
} 
?> 
相關問題