Appcelerator push notifications ACS REST API with java

607 views Asked by At

I'm trying to send push notifications via Appcelerator rest API through my java server. I've been able to login, but when I try to send the notification I get 422 error (Unprocessable entity)

Here´s my login:

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);
    }

Then I try to send the notification

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;

}

In the second part I got 422 Error.

1

There are 1 answers

0
Josinaldo Cipriano Fontes On

I got the same problem. I fixed changing to Admin: yes the user that I'm using.

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