2011-12-17 140 views
0

我已經能夠推送消息給C2DM服務器

  1. 獲取服務器authrization,並保存到數據存儲;
  2. 將手機註冊到c2dm服務器;
  3. 將該ID發送到將應用程序c2dm regeistration id保存到數據存儲的應用程序服務器。

現在我只想實現一個servlet,它檢索服務器令牌號。並從數據存儲android應用程序regirstration id並使用它們將消息推送到手機。

這是servlet的代碼:

package com.visd.myfirstapp; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.Date; 

import javax.net.ssl.HostnameVerifier; 
import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.SSLSession; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import com.google.appengine.api.datastore.DatastoreService; 
import com.google.appengine.api.datastore.DatastoreServiceFactory; 
import com.google.appengine.api.datastore.Entity; 
import com.google.appengine.api.datastore.EntityNotFoundException; 
import com.google.appengine.api.datastore.Key; 
import com.google.appengine.api.datastore.KeyFactory; 

//import com.visd.myfirstapp.MessageUtil.CustomizedHostnameVerifier; 

public class Visd extends HttpServlet { 
    private final static String AUTH = "authentication"; 

    private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth"; 

    public static final String PARAM_REGISTRATION_ID = "registration_id"; 

    public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle"; 

    public static final String PARAM_COLLAPSE_KEY = "collapse_key"; 

    private static final String UTF8 = "UTF-8"; 
    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
      throws IOException 
    { 
     resp.setContentType("text/plain"); 
     Entity appRegIdEntity = null; 
     Entity serverTokenEntity = null; 
     int RetCode = 0; 
     String message = "Congrats C2DM process completed"; 
     Key appRegIdKEY = KeyFactory.createKey("c2dmreg","cr"); 
     Key serverTokenKEY = KeyFactory.createKey("vToken", "tokenkn"); 
     DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 
     String appRegId = null, serverToken = null, chk =null; 
     try { 
      appRegIdEntity = datastore.get(appRegIdKEY); 
      serverTokenEntity = datastore.get(serverTokenKEY); 
      serverToken = (String) serverTokenEntity.getProperty("token"); 
      appRegId = (String) appRegIdEntity.getProperty("c2dmid"); 


      RetCode = sendMessage(serverToken, appRegId, message); 

     } catch (EntityNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      chk = "Entered In Exception";  

     } 
     resp.getWriter().println("Return code :" + RetCode + "chk value :" + chk); 
    } 



    // Message Sending method 

    public static int sendMessage(String auth_token, String registrationId, String message) throws IOException 
    { 

     StringBuilder postDataBuilder = new StringBuilder(); 
     postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId); 
     postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0"); 
     postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message, UTF8)); 

     byte[] postData = postDataBuilder.toString().getBytes(UTF8); 



     URL url = new URL("https://android.clients.google.com/c2dm/send"); 
     //HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());//commented as was causing error, i dont know why 
     HttpsURLConnection conn = (HttpsURLConnection) 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="+ auth_token); 

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

     int responseCode = conn.getResponseCode(); 
     return responseCode; 
    } 


} 

但瀏覽器總是顯示RETCODE = 0和清潔香港值=「輸入的在異常」 ieIt從未將消息發送到機器人裝置,代替總是進入例外狀態。代碼有什麼問題,我找不出來。
請幫忙。 謝謝。

+0

請編輯您的帖子,幷包括`e.printStackTrace()對應的堆棧跟蹤;`。 – 2012-01-05 16:50:58

回答

0

這是怎麼了,我終於解決了,代碼幫助: -

public class C2dmsender { 
    public static String send(String regid, String appRegId, String mtype, String[] message) throws UnsupportedEncodingException 
    { 


     String serverToken = ""//give the sever token here; 
    data.append("registration_id=" + appRegId);//appRegId is the C2DM id of the device in which you want to push 

    // Collapse key is for grouping messages and only the last sent message 
    // with the same key going to be sent to the phone when the phone is 
    // ready to get the message if its not from the beginning 
    data.append("&collapse_key=test"); 

    // Here is the message we sending, key1 can be changed to what you whant 
    // or if you whant to send more then one you can do (i think, not tested 
    // yet), Testing is the message here. 
    data.append("&data.key1="); 

    // If you whant the message to wait to the phone is not idle then set 
    // this parameter 
    // data.append("&delay_while_idle=1"); 

    byte[] postData = data.toString().getBytes("UTF-8"); 


    try { 
     // Send data 
     URL url = new URL("https://android.apis.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=" 
       + serverToken); 

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

     Integer responseCode = conn.getResponseCode(); 
     if (responseCode.equals(503)) { 
      // the server is temporarily unavailable 

     } else { 
      if (responseCode.equals(401)) { 
       // AUTH_TOKEN used to validate the sender is invalid 

      } else { 
       if (responseCode.equals(200)) { 

        // Check for updated token header 
        String updatedAuthToken = conn 
          .getHeaderField("Update-Client-Auth"); 
        if (updatedAuthToken != null) { 
         serverToken = updatedAuthToken; 

        } 

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


      } 
     } 
    } catch (Exception e) { 

     } 


    } 

}