极光推送服务端搭建

阅读更多
static int a = 1000;
final static String URL = "http://api.jpush.cn:8800/v2/push";//极光推送url
final static String APP_KEY = "b1bb2f134c75b20c0566d953";//极光推送申请的appkey
final static String MASTER_SRCRET = "dfa841a7caa7b43ef68de578";//md5验证key
 
/**
* 
* @param receiverType
* @param alias //在手机端设置的别名
* @param receiverValue
* @param msgType
* @param msgContent
*/
public static void pushMessage(String receiverType,String alias,String receiverValue,String msgType,
String msgContent){
       HttpPost httpRequest = new HttpPost(URL);
       a++;
       List params = new ArrayList();
       params.add(new BasicNameValuePair("sendno", a + ""));
       params.add(new BasicNameValuePair("app_key", APP_KEY));
       params.add(new BasicNameValuePair("receiver_type", receiverType));
       params.add(new BasicNameValuePair("alias", alias));
       params.add(new BasicNameValuePair("receiver_value", receiverValue));
       params.add(new BasicNameValuePair("msg_type", msgType));
       int sendno = a;
      // int receiverType = 3;
       //String receiverValue = "wang";
     //  String masterSecret = "dfa841a7caa7b43ef68de578"; 
                                                         
 
       String input = String.valueOf(sendno) + receiverType + receiverValue + MASTER_SRCRET;
       MD5 md5 = new MD5();
       String verificationCode = md5.getMD5ofStr(input);
       System.out.println("verification_code:" + verificationCode);
       params.add(new BasicNameValuePair("verification_code", verificationCode));
       
       params.add(new BasicNameValuePair("msg_type", msgType));// String.valueOf(msg_tpye)));
       JSONObject jsonObject = new JSONObject();
 
       if (Integer.valueOf(msgType) == 1)
       {
           jsonObject.put("n_content",msgContent);
       }
       else
       {
           jsonObject.put("message", msgContent);
       }
       System.err.println("jsonObject" + jsonObject.toString());
       params.add(new BasicNameValuePair("msg_content", jsonObject.toString()));
      // params.add(new BasicNameValuePair("send_description", "ren cai a "));
       params.add(new BasicNameValuePair("platform", "android"));
       params.add(new BasicNameValuePair("time_to_live", "0"));
 
       try
       {
           httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
           HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
           if (httpResponse.getStatusLine().getStatusCode() == 200)
           {
               String strResult = EntityUtils.toString(httpResponse.getEntity());
               System.out.println("get Date :" + strResult);
               return;
           }
 
       } catch (Exception e)
       {
           e.printStackTrace();
       }
   }
 

 

你可能感兴趣的:(java,极光推送,服务端)