开发者

Push message in Android

I have managed the get the auth Token from google and now I am trying to push the message to my android device.

//code to fetch auth Token
<?php

$url = "https://www.google.com/accounts/ClientLogin";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
echo $url;
$post_fields = "accountType=" . urlencode('HOSTED_OR_GOOGLE'). "&Email=" . urlencode('mail@gmail.com'). "&Passwd=" . urlencode('xxx'). "&service=" . urlencode('ac2dm');
echo $post_fields; echo "<br/>";
$data = array('accountType' => 'GOOGLE', 'Email' => 'mail@gmail.com', 'Passwd' => 'xxx', 'source'=>'PHI-cUrl-Example', 'service'=>'ac2dm');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request
var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT)); //for debugging the request
$response = curl_exec($ch);
var_dump($response);
curl_close($ch);
echo "<br/><br/>";
?>

I get the AuthToken from google. Now I need to send my message to android device This is the php code for that...

(second.php)

<?php
$wannasay = array (
"registration_id" => "APA91bH9SYn9Iru4Ddr87neqEh3OWaUhT8k_UUpxUZj7ORMLwFCiZ3qKm1yKWGFro47DNsk6CynvureInynXFqs1zEZ9xUwUlw",
"collapse_key" => "abcdef"
);
$dataels = array();
fore开发者_如何转开发ach (array_keys($wannasay) as $thiskey) {
array_push($dataels,urlencode($thiskey) ."=".
urlencode($wannasay[$thiskey]));
}
$data = implode("&",$dataels);

// work out the request
//auth token below is the one i got from above script


$authToken="DQAAAK0AAAABGiOYfUQfp_ZiDyZqErKOacSmgZCFvnogDUaVzSX9a6qh079XJblM63Z12JwOdRv8gDNCqLj80olSbFeqZFJpqXQDvmmW_PcyHUZPyDZIpZ0G0FRkV_sgqy06YM7r5zUnAggHA8hlmVM3AQBYNGcP_A2Ddq_pjm6cqaQBQH_dFaJgK6UfxNaNDK2jl78VWSU-ZMb-Q2zU1xMLbwYcWXuYIRqRxSlEN68N2M2u4Ub_AA";
$header =
"POST http://android.apis.google.com/c2dm/send HTTP/1.1\n" .
"Host: yourhost\n" .
"Content-Type: application/x-www-form-urlencoded\n" .
"Authorization: GoogleLogin auth=" . $authToken . "\n" .
"Content-Length: " . strlen($data) . "\n\n" .
$data . "\n";


// establish the connection and send the request

$s = socket_create(AF_INET, SOCK_DGRAM, 0);//AF_INET ipv4 internet based protocol
$z = socket_connect($s, gethostbyname("yourhost"), 80);
socket_write ($s, $header, strlen($header));

$header = "";

while (true) {   
if (strlen($c = socket_read($s, 1))) {  //this is line 20
$header .= $c; 
}
}

socket_close($s);
print nl2br(htmlspecialchars($header));

?> 

I am running both the code from localhost. I am having socket problem here I think. Either the page keeps loading forever or I gives me fatal error : maximum time exceeded 60 seconds at line 20 or so.

When I try the code at server I get the error as

Warning: socket_read() [function.socket-read]: unable to read from socket [111]: Connection refused in second.php on line 20

I dont know what is wrong here. Can someone tell me why am I getting this error. I have no idea how the socket thing works on Android as well.


You said you're running it on localhost (I assume you mean on a computer and not the phone)

If you're running the emulator, it actually has it's own IP, so localhost points to your emulator, not your computer. You'll have to use your computer's IP address (Usually 192.168.x.x if you're behind a router).

If you're running on a phone you'll also have to use your computer's IP address, but you'll have to use your external IP not the one behind your router (the phone isn't on your local network).

That being said, I noticed you have this line (no numbers, but its near the end)

socket_connect($s, gethostbyname("yourhost"), 80);

I'm guessing by "yourhost" you meant "localhost", and by that you meant your computer's IP address :)

Actually, I notice you have "yourhost" used a few times, was that just a mistake from copying from a tutorial or documentation page?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜