Appcelerator推送通知ACS REST API与java

时间:2015-06-01 17:59:34

标签: java push-notification appcelerator appcelerator-acs

我正在尝试通过我的java服务器通过Appcelerator rest API发送推送通知。我已经能够登录,但是当我尝试发送通知时,我得到422错误(不可处理的实体)

这是我的登录信息:

String SENDER_ID = "55694f177eead29359bda190";
String API_KEY = "bRhpzjfpHakUkYeVGbCBoFLGpqLTeKIm";
String API_USR = "tuin";
String API_PAS = "tuin123";
String URL_ACS = "https://api.cloud.appcelerator.com/v1/";
URL url = null;
URLConnection uc = null;
String idSession=null;
    try {
         url = new URL(URL_ACS+"users/login.json?key="+API_KEY+"&login="+API_USR+"&password="+API_PAS+"");
         uc = url.openConnection();
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("POST");
         if (conn.getResponseCode() != 200) {
           throw new Exception(conn.getResponseMessage());
         }
         InputStream is = conn.getInputStream();
         BufferedReader rd = new BufferedReader(
             new InputStreamReader(is));
         StringBuilder sb = new StringBuilder();
         String line;
         while ((line = rd.readLine()) != null) {
           sb.append(line);
         }
         rd.close();
         conn.disconnect();

        String respuesta=sb.toString();
        if(respuesta.contains("status\":\"ok") && respuesta.contains("code\":200") ){
                int number=sb.indexOf("session_id");
                String meta=sb.substring(number+13, number+60);
                int fin=meta.indexOf("\"");
                idSession=meta.substring(0, fin);
            }else{
                System.out.println("No ID");
            }
    } catch (Exception e) {
        throw new Exception("Trouble "+e);
    }

然后我尝试发送通知

public String sendPush(String date,String name, String text, String title,String session_id) throws Exception{

String URL_ACS = "https://api.cloud.appcelerator.com/v1/";
String API_KEY = "bRhpzjfpHakUkYeVGbCBoFLGpqLTeKIm";
URL url = null;
HttpURLConnection uc = null;
String idSession=null;
try {

     String rt=URL_ACS+"push_notification/notify.json?key="+API_KEY+"";
     url=new URL(rt);
     uc = (HttpURLConnection) url.openConnection();

     uc.setDoInput(true);
     uc.setDoOutput(true);
     uc.setRequestProperty("Content-Type", "application/json");
     uc.setRequestProperty("Accept", "application/json");
     uc.setRequestProperty("Cookie","_session_id="+session_id);

     JSONObject cred = new JSONObject();
     JSONObject push = new JSONObject();
     JSONObject chan = new JSONObject();

     cred.put("alert","test");
     cred.put("title","title");
     cred.put("icon","icon_notifi");
     cred.put("vibrate",true);
     cred.put("sound","default");
     push.put("payload",cred); 

     //chan.put("push_notification", push);

     System.out.println(push.toString());
     String responseJSON=push.toString().replace("{\"payload\":", "{channel=\"noti\",to_ids=\"everyone\",payload=");
     OutputStreamWriter wr= new OutputStreamWriter(uc.getOutputStream());
     wr.write(responseJSON);

     if (uc.getResponseCode() != 200) {
       throw new Exception(uc.getResponseMessage());
     }
     InputStream is = uc.getInputStream();
     BufferedReader rd = new BufferedReader(
         new InputStreamReader(is));
     StringBuilder sb = new StringBuilder();
     String line;
     while ((line = rd.readLine()) != null) {
       sb.append(line);
     }
     rd.close();
     uc.disconnect();

     System.out.println("The content was: " + sb.toString());

} catch (Exception e) {
    throw new Exception("Trouble: "+e);
}
return idSession;

}

在第二部分我得到了422错误。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我修改了管理员:是我正在使用的用户。

http://docs.appcelerator.com/arrowdb/latest/#!/guide/admin_access