I'm sending a POST request through Retrofit2 in Android to FCM creating Device Group Notification.
This is my RequestModel:
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class FirebaseMessagingDeviceGroupRequestModel {
@SerializedName("operation")
@Expose
private String operation;
@SerializedName("notification_key_name")
@Expose
private String notificationKeyName;
@SerializedName("registration_ids")
@Expose
private List<String> registrationIds;
public void setNotificationKeyName(String notificationKeyName) {
this.notificationKeyName = notificationKeyName;
}
public void setOperation(String operation) {
this.operation = operation;
}
public void setRegistrationIds(List<String> registrationIds) {
this.registrationIds = registrationIds;
}
public String getOperation() {
return operation;
}
public String getNotificationKeyName() {
return notificationKeyName;
}
public List<String> getRegistrationIds() {
return registrationIds;
}
}
This is the Response Model:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class FirebaseMessagingDeviceGroupResponseModel {
@SerializedName("notification_key")
@Expose
private String notificationKey;
public String getNotificationKey() {
return notificationKey;
}
public void setNotificationKey(String notificationKey) {
this.notificationKey = notificationKey;
}
}
This is the Retrofit2 API Client Interface:
@Headers({
"Content-Type: application/json",
"Authorization: key=AAAA2kYlOBY:APA91bGPnbTqQIYw21Mtyg36.......",
"project_id: 937479...."
})
@POST("fcm/notification")
Call<FirebaseMessagingDeviceGroupResponseModel> postNotificationGroupRequest(@Body FirebaseMessagingDeviceGroupRequestModel firebaseMessagingDeviceGroupRequestModel);
This is the MainActivity:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://fcm.googleapis.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitCallbacks callbacks = retrofit.create(RetrofitCallbacks.class);
FirebaseMessagingDeviceGroupRequestModel firebaseMessagingDeviceGroupRequestModel = new FirebaseMessagingDeviceGroupRequestModel();
firebaseMessagingDeviceGroupRequestModel.setNotificationKeyName("sample_group");
firebaseMessagingDeviceGroupRequestModel.setOperation("create");
firebaseMessagingDeviceGroupRequestModel.setRegistrationIds(driverKeys);
Call<FirebaseMessagingDeviceGroupResponseModel> firebaseNotificationGroupRequestCallback = callbacks.postNotificationGroupRequest(firebaseMessagingDeviceGroupRequestModel);
firebaseNotificationGroupRequestCallback.enqueue(new Callback<FirebaseMessagingDeviceGroupResponseModel>() {
@Override
public void onResponse(@NotNull Call<FirebaseMessagingDeviceGroupResponseModel> call, @NotNull Response<FirebaseMessagingDeviceGroupResponseModel> response) {
if (response.body() != null) {
driversNotificationGroupId = response.body().getNotificationKey();
//Processing Notification Key
} else {
Log.e(TAG,"Response Is Empty :: " + response);
}
}
@Override
public void onFailure(@NotNull Call<FirebaseMessagingDeviceGroupResponseModel> call, @NotNull Throwable t) {
t.printStackTrace();
}
});
In Log, I'm getting:
Response Is Empty :: Response{protocol=h2, code=400, message=, url=https://fcm.googleapis.com/fcm/notification
400 error code means my JSON body is not correctly formatted. How can I solve this problem? What is the correct way of sending this JSON body
JSON Body I wanna send (this is the correct one. I've tested it in Postman):
{
"operation":"create",
"notification_key_name":"sample_group",
"registration_ids":["dIL7U3prTWeUHSAvpqmdVs......."]
}
I wanna send the above JSON object using retrofit2 in android app.