我是安卓推送世界的新手,几天来我一直在挣扎。我毫无问题地创建并实现了它的GCM客户端。我还创建了我的谷歌云项目,启用了android推送通知,并获得了我的Project Number
、Project ID
和API Key
。
到目前为止还不错,然后我想开始实现它的服务器端,但由于某种原因,我不知道我应该做什么,我应该在哪里写代码?它会成为像restful这样的web服务吗?我一直在挖http://developer.android.com/google/gcm/ccs.html但我没有得到我应该做什么的好结果。我在互联网上也没有找到合适的教程,它们都是关于像C2DM
这样的推送的不推荐版本。我真的很感激你的帮助。当做
注意:我使用的是Mssql。
我以为开发人员文档中有一个教程是我的服务器的基础。现在我在找它,但找不到,所以我会发布一个代码的精简版本。
我有一个Raspberry Pi作为服务器,运行带有PHP和MySQL数据库的Apache。我选择一个设备和一个项目组合,并通过网页向该设备上的应用程序发送消息。我在PHP中根据项目ID和所有者/设备进行选择,以获得消息将发送到的注册ID。
为了简单起见,我将向您展示如何向RegId和API密钥是硬编码的设备发送消息。你可以稍后把你自己的数据库东西放在它周围。
首先是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服务器。这些脚本在我的树莓派和Windows机器上都能很好地工作。html/php可能有点笨拙,因为我已经做了很多裁剪来去掉我的DB内容,但它至少起作用了。我希望是我们的