Android C2DM 无法工作错误 InvalidRegistration

发布于 2024-12-02 12:58:49 字数 5553 浏览 2 评论 0原文

我正在尝试在 Android 设备上实现 C2DM。我在设备上获取令牌,但每次运行设备时,设备上的 Registration_id 都会发生变化,我不确定它是否应该像那样运行。问题是我无法使用 php curl 脚本向设备发送消息。我收到 InvalidRegistration 错误消息,这意味着 tokenId 根据 c2dm 文档无效,我没有手动传递它是通过脚本生成并传递的。在提出这个问题之前,我已经在所有网站上做了很多研究,这不是一个重复的问题。 我有一个注册的 c2dm 电子邮件 ID。我正在使用这个网站这里的android代码Android代码如下:

ANDROID MANIFEST FILE

<?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 = "[email protected]";
$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 SCRIPT 接收的响应标头

我正在使用安装了curl 扩展的 xampp。

string(318) "HTTP/1.1 200 OK 内容类型:text/plain 日期:2011 年 9 月 1 日星期四 17:04:18 GMT 过期:2011 年 9 月 1 日星期四 17:04:18 GMT 缓存控制:私有,最大-age=0 X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block Server: GSE Transfer-Encoding: chunked Error=InvalidRegistration"

我已经为不同的 android 包/项目注册了不同的电子邮件地址,但尽管我收到了 Registration_id,但没有一个应用程序检索消息。在我的清单中,我已在顶部声明了使用权限,但即使我将它们添加在底部,也不会发生任何变化。

I am trying to Implement C2DM on Android device. I am getting token on device but each time I run the device the registration_id on device changed which Im not sure if it should behave like that. The problem is I am unable to send messages to the device using a php curl script. I am getting InvalidRegistration Error message which means tokenId is invalid according to c2dm documentation which Im not passing manualy it is generated and passed throug script. I have done quite a lot of research on all the sites before asking this question and its not a repeated question. I have a registered c2dm email Id. I am using android code from this site here The --curl and android code is as follow:

ANDROID MANIFEST FILE

<?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 SERVERSIDE SCRIPT

<?php
$registrationId = "789ukisdtfuuskdfh678s98d9f78s9d79f-registrationId from device";
$email = "[email protected]";
$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); 
?>

RESPONSE HEADER RECEIVED FROM PHP CURL SCRIPT

I am using xampp with curl extension installed.

string(318) "HTTP/1.1 200 OK Content-Type: text/plain Date: Thu, 01 Sep 2011 17:04:18 GMT Expires: Thu, 01 Sep 2011 17:04:18 GMT Cache-Control: private, max-age=0 X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block Server: GSE Transfer-Encoding: chunked Error=InvalidRegistration"

I have registered different email addresses for different android packages/projects and but none of the application retreive messages though I am getting the registration_id. In my manifest I have declared uses permission on top but nothing changes even if i add them at bottom.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄司 2024-12-09 12:58:49

如果您收到返回的 ID,则意味着消息已发送到设备,并且问题出在设备端。
如果您的手机使用 wifi 网络而不是 3G,请确保您的防火墙没有阻止设备发送更新请求的端口 5228。

我发送消息的工作代码如下:

使用 C2DM 发送消息的 PHP 代码

<?php
$url = "https://www.google.com/accounts/ClientLogin";
$accountType = 'GOOGLE'; //Doesn't change for this
$email = '[email protected]'; //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);
?>

If you are getting an Id back then that means message has been sent to the device and the problem is on device side.
If you phone is using wifi network instead of 3G then make sure that the your firewall is not bloking the port 5228 on which the device will send request for updates.

I working code for sending message is as follow:

PHP CODE FOR SENDING MESSAGES USING C2DM

<?php
$url = "https://www.google.com/accounts/ClientLogin";
$accountType = 'GOOGLE'; //Doesn't change for this
$email = '[email protected]'; //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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文