PHP:如何在一个帖子中向多个设备发送GCM通知

PHP:如何在一个帖子中向多个设备发送GCM通知,php,android,push-notification,google-cloud-messaging,Php,Android,Push Notification,Google Cloud Messaging,我是一名android开发人员,目前正在使用PHP在GCM服务器端工作。从我的客户端,我将gcm响应令牌存储在一个数据库中,现在我想一次性向注册用户发送消息。我不知道该怎么做,我完全糊涂了。我的简单HTML文件是: <html> <head> <title> GCM </title> </head> <body topmargin="50" leftmargin="50" CLASS = "bg"> <form act

我是一名android开发人员,目前正在使用PHP在GCM服务器端工作。从我的客户端,我将gcm响应令牌存储在一个数据库中,现在我想一次性向注册用户发送消息。我不知道该怎么做,我完全糊涂了。我的简单HTML文件是:

<html>
<head>
<title>
GCM
</title>
</head>
<body topmargin="50" leftmargin="50" CLASS = "bg">
<form action="send_message.php" name="myform" method="post">
<h4>Admin for sending notificaion to the registered user</h4>
Notification Text: <input name="appText" size="15" type="text"/><br>
URL: <input name="url" size="15" type="text" /><br>
<button type="submit">Submit</button>
</form>
</body>
</html>

GCM
管理员,用于向注册用户发送通知
通知文本:
网址:
提交
还有,我的send_message.php:

<?php
error_reporting (0);

$link = mysql_connect("localhost","db","db"); 
if (!$link) 
 { 
 die(mysql_error()) ; 
 } 
mysql_select_db("gcm_user")or die(mysql_error());
........................................
.....................................
.....................................
?>

现在,在php文件中,我应该怎么做,我已经尝试了这个链接

刚才,现在是通过这个链接来的。请建议我如何使用此解决方案解决我的问题


但是我只想要一个php。请帮助克服这个问题。

这些链接似乎使PHP过于复杂。您只需使用查询中的值填充$registrationIDs数组。我不是PHP专家,但我使用了经常引用的CURL示例并准备了如下语句:

   $ttl = 86400;
   $randomNum=rand(10,100); 
   $registrationIDs = array();
   .....

   $queryregid = "select regid from registrations where  YOUR SPECIFIC CRITERIA......";
   if (!($stmt = $mysqli->prepare( $queryregid))) {
      echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; }
   if (!$stmt->execute()) {  
      echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; }
   if (!$stmt->bind_result($dev_i)){  
      echo "bind failed: (" . $stmt->errno . ") " . $stmt->error;  }

   /* fetch values */
   while ($stmt->fetch()) {
      $regidOne =  $dev_i;
      $registrationIDs[] = $regidOne; // Fill the array
   }
   $stmt->close();

   $fields = array(
           'registration_ids' => $registrationIDs,
             'data' => array( "message" => $message),
             'delay_while_idle'=> false,
             'time_to_live' => $ttl,
             'collapse_key'=>"".$randomNum.""
            );
   $headers = array(
        'Authorization: key=' . $apiKey,
         '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 );
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));

   // Execute post
   $result = curl_exec($ch);
   // Close connection
   curl_close($ch);

回答我自己的问题。以下是我为满足我的要求所做的工作

$gcm_text = $_POST['gcmText'];
$gcm_url = $_POST['gcmURL'];
$gcm_subText = $_POST['gcm_secondText'];

$sql = "select gcm_response_token as regId from GCM";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    $gcm_array[]=$row['regId'];
    $counter++;
}

mysql_free_result($result);


    $body['allsearch'] = array(
            'gcmText' => $gcm_text,
            'gcm_secondText' => $gcm_subText,
            'gcmURL' => $gcm_url
            );

    // Set POST variables
  $url = 'https://android.googleapis.com/gcm/send';

  $fields = array(
 'registration_ids' => $gcm_array,
 'data' => $body,
 );

//echo GOOGLE_API_KEY;
 $headers = array(
 'Authorization: key=$apiKey',
  '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;

?>

这样,我就可以将json数据发送到客户端并获得通知。

您能更清楚地告诉我这些信息吗。我是PHP新手,不知道怎么做。你有没有任何简单的例子来满足我的要求。我面临一个奇怪的问题,我有3台设备,但只在1台设备中接收,其他设备状态显示成功,设备r也连接到internet,尽管没有收到。。我希望我的代码可以很好地工作,因为它可以在一台设备上工作,有什么线索为什么不能在其他两台设备上接收吗?试试这个例子来制作服务器。我应该在哪里设置TTL值?