Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

How to send Push Notification using java and ACS ???

$
0
0

Hi, im trying to send notifications from a java server, so far i got:

package com.tuinapps.tirolibre.model;
import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection;
import org.json.JSONObject;
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter;
public class ConsultaACS {
public String SENDER_ID = "55694f177eead29359bda190";
public static String API_KEY = "bRhpzjfpHakUkYeVGbCBoFLGpqLTeKIm";
public static String API_USR = "tuin";
public static String API_PAS = "tuin123";
public static String URL_ACS = "https://api.cloud.appcelerator.com/v1/";
 
 
public static void main(String[] args){
 
 
}
 
public String obtieneIdSession() throws Exception{
    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();
        System.out.println("::::::::::::::RESPUESTA "+respuesta);
        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);
 
                System.out.println("OOOOOOOOOOOOOOOOOOSession Id :"+idSession);
            }else{
                System.out.println("No encuentra");
            }
    } catch (Exception e) {
        throw new Exception("Problema de session "+e);
    }
    return idSession;
}
 
public String enviaMensaje(String fecha,String nombre, String texto, String titulo,String session_id) throws Exception{
    URL url = null;
    HttpURLConnection uc = null;
    String idSession=null;
    try {
 
         String ruta=URL_ACS+"push_notification/notify.json?key="+API_KEY+"";
 
 
 
 
 
         url=new URL(ruta);
         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","Sample alert");
         cred.put("title","Pruebas");
         cred.put("vibrate",true);
         cred.put("sound","default");*/
 
         push.put("payload",cred);
// push.put("channel","All users"); chan.put("push_notification", push);
         System.out.println(push.toString());
 
         //String respuestaJSON=push.toString().replace("{\"payload\":", "{\"channel\":\"noti\",\"to_ids\":\"everyone\"");
 
        // String respuestaJSON="{\"channel\":\"noti\",\"to_ids\":\"everyone\"}";
 
         //System.out.println("HHHHHHHHH PRIMERA RESPUESTA JSON: "+respuestaJSON);
 
         System.out.println("HHHHHHHHH PRIMERA RUTA: "+ruta);
 
         OutputStreamWriter wr= new OutputStreamWriter(uc.getOutputStream());
         wr.write(push.toString());
 
 
 
 
         if (uc.getResponseCode() != 200) {
             System.out.println("HHHHHHHHH resposnse code: "+uc.getErrorStream()+"HHHHHHHHH uce: "+uc);
           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("Problema de mensaje "+e);
    }
    return idSession;
}
}

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>