我是一名 Android 开发人员,目前使用 PHP 在 GCM 服务器端工作。在我的客户端,我将 gcm 响应 token 存储在一个数据库中,现在我想一次性向注册用户发送消息。我无法弄清楚该怎么做,而且我完全感到困惑。我的简单 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>
还有,我的 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 文件中我应该做什么,我试过这个链接 Android Hive example
只是,现在通过这个链接来了。请建议我如何使用此解决方案来解决我的问题。 New link for my problem.
但我只需要一个 php 来满足我的目的。请帮助克服这个问题。
最佳答案
回答我自己的问题。以下是我为满足要求所做的工作。
$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:如何在一个帖子中向多个设备发送 GCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599110/