본문 바로가기

Programming/과거포스팅

C2DM Message Send (Server logic)

String param = "";

param  = "registration_id="+registration_id;
param += "&collapse_key="+collapse_key;
param += "&data.identity="+collapse_key;
param += "&data.mdn="+"0100000000";
param += "&data.payload="+data_payload;
param += "&data.time="+String.valueOf(System.currentTimeMillis());
param += "&delay_while_idle="+delay_while_idle;
System.out.println("param : " + param) ;
byte[] postData = param.getBytes("UTF-8");
try
{
    String rtnValue = "";
    URL url = new URL("http://android.apis.google.com/c2dm/send");
    HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);
        
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("Content-Length", Integer.toString(param.length()));
        urlConnection.setRequestProperty("Authorization", "GoogleLogin auth="+auth);
        
        
        OutputStream os = urlConnection.getOutputStream();
        os.write(postData);
        os.close();
        
        urlConnection.connect();
        
        int responseCode = urlConnection.getResponseCode();
        System.out.println("responseCode : " +  responseCode);
        
        if(responseCode == HttpURLConnection.HTTP_OK)
        {
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String readLine;
            while ((readLine = reader.readLine()) != null) {
                System.out.println(readLine);
            }
            reader.close();
            
        }
        urlConnection.disconnect();
        
    
    
}catch(Exception e)
{
    System.out.println(e.toString());
}
}