My app crashed while trying to send fcm notifications to devices subcribed to a topic using android volley Json, Here is the code am using.
private void sendNotify()throws JSONException{
rootObject = new JSONObject();
JSONObject dataObject = new JSONObject();
dataObject.put("message_title", "This is the Title");
dataObject.put("message", "This is the Body Of The Message");
dataObject.put("message_image_url", "https://www.image.com/image.jpg");
String url0 = "https://fcm.googleapis.com/fcm/send";
try{
rootObject.put("data", dataObject);
rootObject.put("to","/topics/DailyNotice");
} catch (JSONException e){
Toast.makeText(this, "Error 1"+e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
StringRequest request = new StringRequest(Request.Method.POST, url0, response -> Toast.makeText(getApplicationContext(), "Response "+response, Toast.LENGTH_SHORT).show(), error -> {
Toast.makeText(this, "Error 1"+error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}){
@Override
public byte[] getBody() {
return rootObject.toString().getBytes();
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "key="+dKey);
return headers;
}
} ;
requestQueue.add(request);
}
when i send using this above method i get this response string in toast >> {message_id:"some random numbers"}.