2012-02-18 86 views
1

我已经实现了从C2DM服务器请求注册ID的C2DM客户端。一旦应用程序获得注册ID,应用程序将设备ID和注册ID存储在服务器数据库中我已经实现了一个PHP服务器,但是当我尝试发布消息时,我得到Error = MissingRegistration。Android C2DM缺少注册错误?

PHP服务器代码

$url = 'https://www.google.com/accounts/ClientLogin'; 

$body = '[email protected]&Passwd=password&accountType=GOOGLE&source=MyLittleExample&service=ac2dm'; 
$c = curl_init ($url); 
curl_setopt ($c, CURLOPT_POST, true); 
curl_setopt ($c, CURLOPT_POSTFIELDS, $body); 
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true); 
$page = curl_exec ($c); 
$p = explode("Auth=",$page); 
curl_close ($c); 

$url = 'https://android.apis.google.com/c2dm/send'; 

$param = // Get Registration id from my sql 
$mtype = 'important'; 
$msg = 'Hello'; 

$len = strlen($param); 
echo $len; 
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) { 

     $headers = array('Content-length:300','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; 
    } 
$data = sendMessageToPhone($p[1],$param,$mtype,$msg); 
var_dump($data); 

根据该文件错误= MissingRegistration如果在发布采购信息不registration_id参数发生。我甚至试图通过对注册ID进行硬编码来运行应用程序。任何帮助不胜感激。谢谢。

+0

结构清晰,明确的问题。我甚至不知道PHP,但我仍然可以理解一切。值得+1。仍然在寻找问题。 – 2012-02-18 10:04:39

+0

@Boris谢谢:) – iNan 2012-02-18 10:06:47

回答

1

啊哈!这里提供了可能的解决方案(从关于来自另一种我不知道的语言的通知的线索中获取):https://stackoverflow.com/a/8339611/1108032。请阅读他所说的解决他的问题。对我来说,看起来像这样的应该会让你失望,因为代码上你的代码似乎没问题。

+0

@谢谢你,bt仍然在努力:( – iNan 2012-02-18 10:27:08

+0

你从这篇文章尝试了什么?你有没有尝试再次注册你的设备,以便刷新注册ID? – 2012-02-18 10:44:00

+0

你试过,其实我试过后发现了错误,即如果我在curl中设置标题,post参数不会发送到服务器。再次感谢:) – iNan 2012-02-18 10:56:37