我想在Android設備上實現C2DM。我在設備上獲得令牌,但每次運行設備時,設備上的registration_id都發生了變化,我不知道它是否應該像這樣。問題是我無法使用php curl腳本將消息發送到設備。我得到InvalidRegistration錯誤消息,這意味着tokenId根據c2dm文檔是無效的,我不通過manualy它生成並通過腳本傳遞。在提出這個問題之前,我已經在所有的網站上進行了大量的研究,而且這個問題不是一個重複的問題。 我有註冊的c2dm郵箱編號。我使用Android的代碼從本網站here的--curl和Android的代碼如下:Android C2DM不工作錯誤InvalidRegistration
Android清單文件
<?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捲曲的服務器端腳本
<?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 Content-Type:text/plain日期:2011年9月1日星期四17:04:18 GMT過期時間: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「
我已經註冊了不同的android包/項目的不同的電子郵件地址,但沒有應用程序檢索郵件,雖然我得到registration_id。在我的清單中,我已經聲明瞭使用權限,但是即使我將它們添加到底部,也沒有任何更改。
PHP代碼看起來與驗證關鍵的一個問題。您是否嘗試過對網址進行編碼? (urlencode($ Auth)) – dermatthias
它給我401錯誤當我做urlencode($ Auth) – user606669
我得到了同樣的問題...請別人幫我.... – Karthik