我对android的推动世界一无所知,我已经挣扎了几天了。我创建并实现了它的GCM客户端,没有问题。我还创建了google项目,启用了android通知,并获得了我的Project Number、Project ID和API Key。
到目前为止,我想要实现它的服务器端,但出于某种原因,我不知道我应该做什么,我应该在哪里编写代码?它将是一个像restful一样的web服务吗?我一直在挖掘http://developer.android.com/google/gcm/ccs.html,但我没有得到好的结果,我应该做什么。我还没有在互联网上找到关于这方面的适当教程,它们都是关于推的不受欢迎的版本,比如C2DM。我真的很感谢你的帮助。致以问候。
注意:我使用的是Mssql。
发布于 2014-02-20 16:08:01
我认为在开发人员文档中有一个教程可以作为我的服务器的基础。现在我寻找它,我找不到它,所以我将张贴我的代码削减版本。
我有一个Raspberry作为服务器,使用PHP和MySQL数据库运行Apache。我选择一个设备和一个项目组合,并通过网页向该设备上的应用程序发送消息。我根据项目ID和所有者/设备在PHP中进行选择,以获得将消息发送给我的注册ID。
为了简单地回答这个问题,我将向您展示如何向RegId和API密钥硬编码的设备发送消息。稍后,您可以将您自己的DB内容放在它周围。
首先是vals.php文件,该文件保存了我/您的秘密硬编码内容。
<?php
$regidOne="Your regid for one phone/project combination";
$apiKey = "Your API KEY";
?>其次,包含消息的表单和按钮的entry.php。
entry1.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<?php session_start();?>
<body>
<br>
Compose a message below
<form action="send_it.php" method="post">
<div id="a" align="left">
<P><TEXTAREA name="message" rows="3" cols="58"></TEXTAREA><br>
<INPUT type="submit" value="Click to send the message">
</P>
</div>
</form>
<?php
$ret=$_SESSION['retval'];
echo "Last message status: ";
echo $ret;
echo"<br><br>";
?>
</body>
</html>最后,通过curl完成工作的文件(send_it.php)。
<?php session_start();
require 'vals.php';
$randomNum=rand(10,100);
$registrationIDs[] = $regidOne;
$message = strip_tags($_POST['message']);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message),
'delay_while_idle'=> false,
'time_to_live' => 86400,
'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);
echo "Your message has been sent, the results from the Cloud Server were :\n";
echo "<p>";
echo $result;
$targets = array("{", "\"");
$interim = str_replace($targets, " ", $result);
$pieces = explode(",", $interim);
$pos = strpos($result, 'multicast');
if ($pos === false) {
$ret = "Failed";
$_SESSION['retval']=$ret;
} else {
$ret = "Sent OK, ";
$_SESSION['retval']=$ret.$pieces[0];//Just the multicast id
}
echo "<br>"; echo "JSON parsed"; echo "<br>";
$jres = json_decode($result);
print_r($jres);
$retloc = 'Location:'.$_SERVER['HTTP_REFERER'];
if(!strstr($message, "skipheader")) {
header($retloc);
// COMMENT OUT THE LINE ABOVE TO SEE THE OUPUT, OTHERWISE RETURN TO MESSAGE I/P FORM PAGE
}
?>您将需要一个具有curl支持的Apache web服务器。这些脚本在我的raspberry pi和我的Windows机器上都能很好地工作。html/php可能有点过火,因为我已经做了很多切割来取出我的DB东西,但是它至少可以工作。我希望这是有用的
https://stackoverflow.com/questions/21902217
复制相似问题