时给身体输入错误
我尝试使用邮递员并使用同一JSON对象进行测试,并且在使用Raterofit库时效果很好,但是它不起作用。
我尝试将JSON对象放入POJO类,但仍然不起作用。
notificationservice类
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" +
"}";
}
主要活动
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");
}
});
}
按下按钮时,我正在尝试将通知发送给某些特定用户。从我可以看出的唯一不起作用的是,我在使用Call<ResponseBody> sendNotification(@Body String body)
您应该创建一个模型类
public class MyRequestBodyModel {
@SerializedName("id")
private int id;
@SerializedName("name")
private String name;
}
创建MyRequestBodyModel的新实例并传递您的值
MyRequestBodyModel model = new MyRequestBodyModel();
model.id = 1;
model.name = "john";
将此模型的实例设置为要改造呼叫方法的参数
Call<ResponseBody> sendNotification(@Body MyRequestBodyModel model);