Está en la página 1de 2

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//los relacionamos con los que tenemos en los recursos
especifico = findViewById(R.id.btn_especifico);
atopico = findViewById(R.id.btn_atopico);

//Con el FirebaseMessaging permite subscribirse a tópicos y mandar


mensajes, en esta ocasión a todos los que se subscriban

FirebaseMessaging.getInstance().subscribeToTopic("enviarAtodos").addOnCompleteListe
ner(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(MainActivity.this,"Subscrito a enviar a
todos",Toast.LENGTH_SHORT).show();
}
});

especifico.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {llamarespecifico();}

});
atopico.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
llamaratopico();
}
});
}//fin del onCreate

private void llamaratopico() {


RequestQueue myrequest = Volley.newRequestQueue(getApplicationContext());
JSONObject json = new JSONObject();

try {
//Configuración del token
json.put("to","/topics/"+"enviarAtodos");//A todos los que se
encuentren en el tópico subscritos les llegará el mensaje
JSONObject notificacion = new JSONObject();
//declaramos valores a recibir
notificacion.put("titulo","soy un titulo");
notificacion.put("detalle","soy un detalle");
json.put("data", notificacion);
//declaramos la URL de FireBase
String URL = "https://fcm.googleapis.com/fcm/send";
JsonObjectRequest request = new
JsonObjectRequest(Request.Method.POST,URL,json,null,null){
//Generamos un map

@Override
public Map<String, String> getHeaders() {
Map<String,String> header = new HashMap<>();
header.put("content-type","aplication/json");

header.put("authorization","key=AAAAgP9gN88:APA91bHmddC2LoKlLmWaRKqrsSGSsWchbgIGDew
Lfb_J7PQ-
t89BR96hykzGQSuVMFvnmeyHoh0OMbtSvyBPuM43QheFuEfMc3lRqnyE1Mxiz6e8RgFMzeRz7chgH-
neWgOmR6IdmZE3");
return header;
}
};
myrequest.add(request);
}catch (JSONException e){
e.printStackTrace();
}
}

También podría gustarte