I tried using Postman and testing with the same JSON object and it works totally fine, but it doesn't work when I use the retrofit library.
I tried putting the JSON object in a Pojo class but it still doesn't work.
NOTIFICATIONSERVICE CLASS
public interface NotificationService {
@Headers({
"Content-Type:"+ApiConstants.CONTENT_TYPE,
"Authorization:"+ApiConstants.SERVER_KEY
})
@POST(ApiConstants.SEND)
Call<ResponseBody> sendNotification(@Body String body);
}
APICONSTANTS CLASS
public class ApiConstants {
public static final String SERVER_KEY = "key=mykey";
public static final String SEND = "send";
public static final String BASE_URL = "https://fcm.googleapis.com/fcm/";
public static final String CONTENT_TYPE = "application/json";
public static final String NOTIFICATION = "{\n" +
" \"to\": \"/topics/MISSING\",\n" +
" \"data\": {\n" +
" \"extra_information\": \"This is some extra information\"\n" +
" },\n" +
" \"notification\": {\n" +
" \"title\": \"She pressed the button\",\n" +
" \"text\": \"She misses you\",\n" +
" \"click_action\": \"MAINACTIVITY\"\n" +
" }\n" +
"}";
}
MAIN ACTIVITY
private void buttonIsPressed() {
RetrofitClient retrofitClient;
retrofitClient = RetrofitClient.getInstance();
Call<ResponseBody> call = retrofitClient.getNotificationService().sendNotification(ApiConstants.NOTIFICATION);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
ResponseBody result = response.body();
String gs = new Gson().toJson(result);
Log.d("MainActivity", "response = " + gs);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d(TAG, "//onFailure");
}
});
}
I'm trying to send a notification to some specific users when a button is pressed. From what I can tell the only thing not working is that I give the wrong input for the body when using Call<ResponseBody> sendNotification(@Body String body)