Android C2DM缺少注册错误?

时间:2012-02-18 09:44:17

标签: php android curl android-c2dm

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

PHP服务器代码

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

$body = 'Email=senderid@gmail.com&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);

根据文档,如果Post请求中没有registration_id参数,则会发生Error = MissingRegistration。我甚至试图通过硬编码注册ID来运行应用程序。任何帮助非常感谢。谢谢。

1 个答案:

答案 0 :(得分:1)

啊哈!这里有可能的解决方案(从一个关于我不知道的另一种语言的通知的线程):https://stackoverflow.com/a/8339611/1108032。请阅读他说的解决了他的问题。对我来说,看起来这样的事情应该让你失望,因为代码方面你的代码似乎没问题。