安卓c2dm中的推送通知



我正在使用c2dm在android中开发一个示例推送通知应用程序。以下是我的PHP代码,用于将消息从服务器发送到设备。

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); 
}
sendMessageToPhone("my application server auth token ","my device id","UTF-8","hello");

但我在模拟器上收到了"无信息"通知。我哪里出问题了?请帮帮我。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: GoogleLogin auth=$token", "Content-Length: $len", "Content-Type: application/x-www-form-urlencoded"));
echo curl_exec($ch);
curl_close($ch);

这是我的应用程序用来发送C2DM消息的php代码,其中$data是您的数据数组。请注意,内容长度是必要的,是您的数据长度。

编辑:您可能还发现一些有用的php类,它可以使发送消息变得更好。

最新更新