0
我正在創建一個使用JSON與Web服務器進行通信的安卓遊戲。 當有人開始一個新遊戲而另一個用戶時,我希望該用戶得到一個 即時通知。到目前爲止,我已經完成了通知,他們的工作, ,但它只會在我登錄遊戲時進行檢查,我如何創建一個實時更新 ,即使遊戲關閉,也許會以一分鐘的時間間隔進行檢查?Android Live更新?
c2dm.php
<?
function googleAuthenticate($username, $password, $source="Company-AppName-Version", $service="ac2dm") {
session_start();
if(isset($_SESSION['google_auth_id']) && $_SESSION['google_auth_id'] != null)
return $_SESSION['google_auth_id'];
// get an authorization token
$ch = curl_init();
if(!ch){
return false;
}
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
$post_fields = "accountType=" . urlencode('HOSTED_OR_GOOGLE')
. "&Email=" . urlencode($username)
. "&Passwd=" . urlencode($password)
. "&source=" . urlencode($source)
. "&service=" . urlencode($service);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// for debugging the request
//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request
$response = curl_exec($ch);
//var_dump(curl_getinfo($ch)); //for debugging the request
//var_dump($response);
curl_close($ch);
if (strpos($response, '200 OK') === false) {
return false;
}
// find the auth code
preg_match("/(Auth=)([\w|-]+)/", $response, $matches);
if (!$matches[2]) {
return false;
}
$_SESSION['google_auth_id'] = $matches[2];
}
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) {
$headers = array('Authorization: GoogleLogin auth=' . $authCode);
$data = array(
'registration_id' => $deviceRegistrationId,
'collapse_key' => $msgType,
'data.message' => $messageText //TODO Add more params with just simple data instead
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
?>
C2DMRegistrationReceiver
public class C2DMRegistrationReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Registration Receiver called");
if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
Log.w("C2DM", "Received registration ID");
final String registrationId = intent
.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
Log.d("C2DM", "dmControl: registrationId = " + registrationId
+ ", error = " + error);
// TODO Send this to my application server
}
}
public void sendRegistrationIdToServer(String deviceId, String registrationId) {
Log.d("C2DM", "Sending registration ID to my application server");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("myserverpage");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
// Get the deviceID
nameValuePairs.add(new BasicNameValuePair("deviceid", deviceId));
nameValuePairs.add(new BasicNameValuePair("registrationid", registrationId));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd =
new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
Log.e("HttpResponse", line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
C2DMMessageReceiver
public class C2DMMessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
Log.w("C2DM", "Received message");
final String payload = intent.getStringExtra("payload");
Log.d("C2DM", "dmControl: payload = " + payload);
// Send this to my application server
}
}
}
public void register(View view) {
Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
intent.putExtra("sender", "[email protected]");
startService(intent);
}
現在,如果我開始對一個人的比賽,我有發信息類n到服務器。那我該怎麼做?
Okey我會查找它,謝謝! – simtaxman 2012-04-13 20:44:55
你知道一個很好的教程或什麼?因爲我需要知道如何在服務器端和設備端進行操作。另外,我用php編寫服務器。 – simtaxman 2012-04-13 21:10:17
這是一個很好的例子(目前看起來很糟糕)在這裏:http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html#c2dm_sendmessage。服務器端用Java顯示,所以你必須將它移植到PHP。在網站備份之前,您可以在此處查看緩存版本:http://webcache.googleusercontent.com/search?q=cache:zrU0h--nBpIJ:www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html+ &cd = 1&hl = en&ct = clnk&gl = us – elevine 2012-04-13 21:46:37