[英]Android push service, implementing the gcm server side
我是android push世界的新手,現在已經掙扎了幾天。 我毫無問題地創建並實現了GCM客戶端。 我還創建了我的Google雲項目,啟用了android push notif.s,並獲得了我的Project Number
, Project ID
和API Key
。
到目前為止,一切都很好,然后我想開始實現它的服務器端,但是由於某種原因,我不知道應該做什么,應該在哪里編寫代碼? 它會成為像Restful這樣的Web服務嗎? 我一直在挖掘http://developer.android.com/google/gcm/ccs.html,但我沒有得到應有的結果。 我也沒有在互聯網上找到合適的教程,它們都是關於C2DM
之類的push的過時版本。 我非常感謝您的幫助。 問候。
注意:我正在使用Mssql。
我以為我的服務器基於開發人員文檔中的一個教程。 現在,我正在尋找它,但現在找不到了,所以我將發布代碼的精簡版。
我有一個Raspberry Pi作為服務器,使用PHP和MySQL數據庫運行Apache。 我選擇一個設備和一個項目組合,然后通過網頁將消息發送到該設備上的該應用程序。 我根據項目ID和所有者/設備在PHP中進行選擇,以獲取向其發送消息的注冊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>
最后,文件( send_it.php )通過curl完成工作
<?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可能有點麻煩,因為我做了很多削減工作以取出數據庫的東西,但是至少可以工作。 我希望它有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.