Android C2DM无法正常运行错误InvalidRegistration

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 = "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); 
?>

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.

查看全部
doujugu1722
doujugu1722
2011/09/01 17:50
  • curl
  • android
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复