2011-09-13 62 views
0

以下是我對使用雲到設備消息不能從雲收到消息至設備通訊

1)我登記在谷歌網站使用C2DM,我從他們那裏收到郵件也implememtation。 所以我想我的郵件編號使用正確

2)在我的發射活動我有以下代碼: -

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); 
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new 
    Intent(), 0)); // boilerplate 
    registrationIntent.putExtra("sender", "mail-id"); 
    startService(registrationIntent); 

3)以下是我的廣播接收器的代碼: -

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    Log.d("in on recieve", "++++++++++++"); 
    String a = intent.getAction(); 
    if(a.equals("com.google.android.c2dm.intent.REGISTRATION")){ 
     String regId = intent.getStringExtra("registration_id"); 
     String error = intent.getStringExtra("error"); 
     String unregistered = intent.getStringExtra("unregistered"); 
     if(error!=null){ 

     }else if(regId!=null){ 
      //Toast.makeText(context, regId, Toast.LENGTH_LONG).show(); 
      Intent i = new Intent(context, RegService.class); 
      i.putExtra("reg", regId); 
      context.startService(i); 
     } 
    }else if(a.equals("com.google.android.c2dm.intent.RECEIVE")){ 
     String anothr = intent.getAction(); 
     Toast.makeText(context, anothr, Toast.LENGTH_LONG).show(); 
     Log.e("C2DM", "Message: Fantastic!!!"); 
     // Extract the payload from the message 
     Bundle extras = intent.getExtras(); 
     if (extras != null) { 
      System.out.println(extras.get("payload")); 
      Toast.makeText(context, (CharSequence) extras.get("payload"), Toast.LENGTH_LONG).show(); 
      // Now do something smart based on the information 
     } 
    } 
} 

我能夠通過此獲得註冊ID。然後這個意圖轉到我在下一節中解釋的regService類。

4)以下代碼位於我的RegService類中。它也在清單文件中提到。

DataManager dataManager = DataManager.getInstance(); 
String regId; 
String appId; 
String s; 

public RegService(){ 
    super("Let it be"); 
    //Toast.makeText(getApplicationContext(), "I got the power", Toast.LENGTH_LONG).show(); 
    // TODO Auto-generated constructor stub 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method tub 
    Log.d("In IntentService","++++++++++++++"); 
    regId = intent.getStringExtra("reg"); 
    appId = dataManager.getAppUserId(); 

    TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE); 
    String devId = telephonyManager.getDeviceId(); 
    Log.d("Reg Service", "regID -> "+regId+" devId -> "+devId); 

    authentification(); 
} 

public void authentification(){ 
    HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(
      "https://www.google.com/accounts/ClientLogin"); 
    try { 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
     nameValuePairs.add(new BasicNameValuePair("Email", "MAIL-ID")); 
     nameValuePairs.add(new BasicNameValuePair("Passwd", "piku13nayahai")); 
     nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE")); 
     nameValuePairs.add(new BasicNameValuePair("source", 
       "Google-cURL-Example")); 
     nameValuePairs.add(new BasicNameValuePair("service", "ac2dm")); 

     post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = client.execute(post); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(
       response.getEntity().getContent())); 

     String line = ""; 
     while ((line = rd.readLine()) != null) { 
      Log.e("HttpResponse", line); 
      if (line.startsWith("Auth=")) { 

       s = line.substring(5); 
       Log.d("Authentication Token",s); 
       //Toast.makeText(this, s, Toast.LENGTH_LONG).show(); 
      } 

     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    sendMessage(); 
} 

public void sendMessage(){ 
    try{ 
    StringBuilder postDataBuilder = new StringBuilder(); 
    postDataBuilder.append("registration_id").append("=") 
    .append(regId); 
    postDataBuilder.append("&").append("collapse_key").append("=") 
    .append("0"); 
    postDataBuilder.append("&").append("data.payload").append("=") 
    .append(URLEncoder.encode("Some Message", "UTF-8")); 
    byte[] postData = postDataBuilder.toString().getBytes("UTF-8"); 
    URL url = new URL("https://android.clients.google.com/c2dm/send"); 

    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setDoOutput(true); 
    conn.setUseCaches(false); 
    conn.setRequestMethod("POST"); 
    conn.setRequestProperty("Content-Type", 
      "application/x-www-form-urlencoded;charset=UTF-8"); 
    conn.setRequestProperty("Content-Length", 
      Integer.toString(postData.length)); 
    conn.setRequestProperty("Authorization", "GoogleLogin auth=" 
      + s); 

    OutputStream out = conn.getOutputStream(); 
    out.write(postData); 
    out.close(); 

    int responseCode = conn.getResponseCode(); 

    String responseLine = new BufferedReader(new InputStreamReader(
      conn.getInputStream())).readLine(); 

    Log.d("Response Code from send API",responseLine+"response code ->"+responseCode); 
    // NOTE: You *MUST* use exponential backoff if you receive a 503 
    // response code. 
    // Since App Engine's task queue mechanism automatically does this 
    // for tasks that 
    // return non-success error codes, this is not explicitly 
    // implemented here. 
    // If we weren't using App Engine, we'd need to manually implement 
    // this. 
    if (responseLine == null || responseLine.equals("")) { 
     Log.i("C2DM", "Got " + responseCode 
       + " response from Google AC2DM endpoint."); 
     throw new IOException(
       "Got empty response from Google AC2DM endpoint."); 
    } 

    String[] responseParts = responseLine.split("=", 2); 
    if (responseParts.length != 2) { 
     Log.e("C2DM", "Invalid message from google: " + responseCode 
       + " " + responseLine); 
     throw new IOException("Invalid response from Google " 
       + responseCode + " " + responseLine); 
    } 

    if (responseParts[0].equals("id")) { 
     Log.i("Tag", "Successfully sent data message to device: " 
       + responseLine); 
    } 

    if (responseParts[0].equals("Error")) { 
     String err = responseParts[1]; 
     Log.w("C2DM", 
       "Got error response from Google datamessaging endpoint: " 
         + err); 
     // No retry. 
     throw new IOException(err); 
    } 
}catch (Exception e) { 
    // TODO: handle exception 
    Log.d("In RegService Catch Block","++++++++++++++++++++++++++++++"); 
    e.printStackTrace(); 
} 

} 

現在的問題是我能夠發送消息,我可以從日誌貓本身知道。我收到200本身的響應代碼,並且我能夠發送消息但無法收到它

回答

0

請確保您沒有從您收到的同一電子郵件地址發送。這顯然是周圍的已知問題。

+0

確定我已經改變了電子郵件ID爲u說,但還是老樣子我收到就行字符串responseLine =新的BufferedReader(新的InputStreamReader( \t \t \t \t conn.getInputStream()))一個錯誤的readLine()。我想這是提及請求屬性中的內容長度的問題 – abhishek

+0

好吧我想如何做到這一點與這篇文章http://stackoverflow.com/questions/6564015/httpurlconnection-c-url-openconnection-c-setrequestproperty-沒有工作,但現在我堅持另一個問題,我沒有緩存我的身份驗證令牌,所以現在我引發錯誤09-14 01:57:18.852:WARN/System.err(2000):java.io.IOException:接收到的身份驗證挑戰爲空 – abhishek

+0

您可能想完整編輯問題以反映您當前的問題。或者只是爲此創建一個新的。 – richardwiden

相關問題