Php Android C2DM不工作错误InvalidRegistration
我正在尝试在Android设备上实现C2DM。我正在设备上获取令牌,但每次运行设备时,设备上的注册id都会发生更改,我不确定它是否应该这样做。问题是我无法使用php curl脚本向设备发送消息。我收到InvalidRegistration错误消息,这意味着根据c2dm文档,令牌ID无效,我没有手动传递它是通过脚本生成和传递的。在提出这个问题之前,我已经对所有的网站做了大量的研究,这不是一个重复的问题。我有一个注册的c2dm电子邮件Id。我使用的是本网站的android代码--curl和android代码如下: ANDROID清单文件Php Android C2DM不工作错误InvalidRegistration,php,android,curl,android-c2dm,Php,Android,Curl,Android C2dm,我正在尝试在Android设备上实现C2DM。我正在设备上获取令牌,但每次运行设备时,设备上的注册id都会发生更改,我不确定它是否应该这样做。问题是我无法使用php curl脚本向设备发送消息。我收到InvalidRegistration错误消息,这意味着根据c2dm文档,令牌ID无效,我没有手动传递它是通过脚本生成和传递的。在提出这个问题之前,我已经对所有的网站做了大量的研究,这不是一个重复的问题。我有一个注册的c2dm电子邮件Id。我使用的是本网站的android代码--curl和andro
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mks.android.test" android:versionCode="1"
android:versionName="1.0">
<permission android:name="com.mks.android.test.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mks.android.test.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name=".Alert" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.mks.android.test.C2DMReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.mks.android.test" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.mks.android.test" />
</intent-filter>
</receiver>
</application>
</manifest>
PHP CURL服务器端脚本
<?php
$registrationId = "789ukisdtfuuskdfh678s98d9f78s9d79f-registrationId from device";
$email = "registeredemailwithc2dm@gmail.com";
$password = "abcdef";
$headers = array('Content-Type: application/x-www-form-urlencoded');
$postFields = "Passwd=".$password."&accountType=HOSTED_OR_GOOGLE&source=MKS-TEST-1&service=ac2dm&Email=".$email;
if(!$curl = curl_init()){
die('curl not found');
}
curl_setopt ($curl, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl,CURLOPT_HEADER, TRUE);
curl_setopt ($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE);
$response = curl_exec ($curl);
echo $response;
curl_close ($curl);
$split = explode('Auth=', $response);
$Auth = $split[1];
var_dump($split);
echo "<br/><br/><br/>Auth:".$Auth."<br/>";
$postFields = "registration_id=".$registrationId."&data.message=MessageSentFromServer&collapse_key=storedmessages";
echo '<br/><br>'.$postFields.'<br/></br>';
$headers = array('Content-Type: application/x-www-form-urlencoded', "Content-Length: ".strlen($postFields), "Authorization: GoogleLogin auth=".$Auth);
if(!$curl = curl_init()){
die('curl not found');
}
curl_setopt($curl, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl,CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE);
$response2 = curl_exec ($curl);
curl_close ($curl);
//var_dump($split);
var_dump($response2);
?>
从PHP CURL脚本接收到响应头
<?php
$registrationId = "789ukisdtfuuskdfh678s98d9f78s9d79f-registrationId from device";
$email = "registeredemailwithc2dm@gmail.com";
$password = "abcdef";
$headers = array('Content-Type: application/x-www-form-urlencoded');
$postFields = "Passwd=".$password."&accountType=HOSTED_OR_GOOGLE&source=MKS-TEST-1&service=ac2dm&Email=".$email;
if(!$curl = curl_init()){
die('curl not found');
}
curl_setopt ($curl, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl,CURLOPT_HEADER, TRUE);
curl_setopt ($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE);
$response = curl_exec ($curl);
echo $response;
curl_close ($curl);
$split = explode('Auth=', $response);
$Auth = $split[1];
var_dump($split);
echo "<br/><br/><br/>Auth:".$Auth."<br/>";
$postFields = "registration_id=".$registrationId."&data.message=MessageSentFromServer&collapse_key=storedmessages";
echo '<br/><br>'.$postFields.'<br/></br>';
$headers = array('Content-Type: application/x-www-form-urlencoded', "Content-Length: ".strlen($postFields), "Authorization: GoogleLogin auth=".$Auth);
if(!$curl = curl_init()){
die('curl not found');
}
curl_setopt($curl, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl,CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE);
$response2 = curl_exec ($curl);
curl_close ($curl);
//var_dump($split);
var_dump($response2);
?>
我正在使用安装了curl扩展的xampp
字符串(318)“HTTP/1.1 200 OK内容类型:文本/普通日期:2011年9月1日星期四17:04:18 GMT过期时间:2011年9月1日星期四17:04:18 GMT缓存控制:专用,最大年龄=0 X-Content-Type-Options:nosniff X-Frame-Options:SAMEORIGIN X-XSS-Protection:1;模式=块服务器:GSE传输编码:分块错误=无效注册”
我已为不同的android软件包/项目注册了不同的电子邮件地址,但没有收到任何应用程序检索消息,尽管我正在获得注册id。在我的清单中,我已在顶部声明使用权限,但即使我将其添加到底部,也没有任何更改。如果您获得id,则表示消息已被删除已发送到设备,问题在设备端。 如果您的手机使用的是wifi网络而不是3G网络,请确保您的防火墙没有阻塞设备将发送更新请求的端口5228 I发送信息的工作代码如下: 使用C2DM发送消息的PHP代码
<?php
$url = "https://www.google.com/accounts/ClientLogin";
$accountType = 'GOOGLE'; //Doesn't change for this
$email = 'myemail@gmail.com'; //Enter your Google Account email
$password = 'yourpasswrodforaboveemail'; //Enter your Google Account password
$registrationId = "your-registration-Id- get it from device";
$source = 'companyName-ApplicationName-VersionCode'; //Enter a name for this source of the login
$service = 'ac2dm'; //Select which service you want to log into
//Once that is all done it’s time to use some cURL to send our request and retrieve the auth token:
$ch = curl_init();
$URL = $url."?accountType=".$accountType."&Email=".$email."&Passwd=".$password."&source=".$source."&service=".$service;
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
//Divide the response into an array as to find the auth token
$line = explode("\n", $response);
// close cURL resource, and free up system resources
curl_close($ch);
unset($ch);
unset($response);
$auth_token = str_replace("Auth=", "", $line[2]); //auth token from Google Account Sign In
$messageUrl = "https://android.apis.google.com/c2dm/send";
$collapseKey = "storedmessages";
$data = array('data.message'=>'This is a message'); //The content of the message
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $messageUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$header = array("Authorization: GoogleLogin auth=".$auth_token); //Set the header with the Google Auth Token
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$postFields = array("registration_id" => $registrationId, "collapse_key" => $collapseKey);
$postData = array_merge($postFields, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
//Print response from C2DM service//
print_r($response);
// close cURL resource, and free up system resources
curl_close($ch);
?>
似乎验证密钥有问题。你试过用URL编码吗?围绕它包装一个urlencode()调用,然后重试(urlencode($Auth))当我执行urlencode($Auth)时,它会给我401个错误。我遇到了相同的问题。。。请有人帮帮我…你有解决办法吗?我这里也有同样的问题。有时有效,有时无效。我不明白怎么了。