1

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)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Using this approach to send an FCM message from within your app, requires that you specify the FCM server key within your application. As its name implies, this key should only be used in a trusted environment, such as your development machine, a server you control, or Cloud Functions. By using this key in your app, malicious users can extract it from there, and then send messages to all users of your app on your behalf. To *securely* send messages, you'll need to run server-side code. See https://stackoverflow.com/a/39279716 and https://stackoverflow.com/a/37993724. – Frank van Puffelen Jul 01 '19 at 18:01

1 Answers1

0

you should create a model class

public class MyRequestBodyModel {

    @SerializedName("id")
    private int id;

    @SerializedName("name")
    private String name;

}

create a new instance of MyRequestBodyModel and pass your value

MyRequestBodyModel model = new MyRequestBodyModel();
model.id = 1;
model.name = "john";

set instance of this model as arguments to retrofit call method

Call<ResponseBody> sendNotification(@Body MyRequestBodyModel model);