0

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.

S. Joshi
  • 153
  • 1
  • 3
  • 14
  • 1
    Your baseUrl("https://fcm.googleapis.com/") is not correct. You should use: https://fcm.googleapis.com/fcm/notification as much as I know – SlothCoding Jan 30 '21 at 12:50
  • fcm/notification is being assigned from API Client Interface – S. Joshi Jan 30 '21 at 13:30
  • 1
    Can you print your firebaseMessagingDeviceGroupRequestModel and check before sending what it is? Sorry for the above question, I didn't see it. – SlothCoding Jan 30 '21 at 15:48
  • 1
    *firebaser here* Calls to the FCM REST API require that you specify the FCM *server** key in your code. As its name implies, this key should only be used in server-side code, or in an otherwise trusted environment. The reason for this is that anyone who has the FCM server key can send whatever message they want to all of your users. By including this key in your Android app, a malicious user can find it and you're putting your users at risk. See https://stackoverflow.com/a/37993724 for a better solution. – Frank van Puffelen Jan 30 '21 at 16:12
  • @FrankvanPuffelen Thank You for your help. As per your suggestion, I've deployed a Cloud Function for handling the FCM related operations and sending the messages in multicast. That does solve the purpose I was using the the retrofit the way I've posted in the question above. Still, I'm curious and looking for reason, why I was getting 400 error then. – S. Joshi Jan 30 '21 at 19:18
  • 1
    @SlothCoding Thank you for your help. I've solved the issues by deploying nodejs function for handling FCM related operations on Firebase Cloud Function. – S. Joshi Jan 30 '21 at 19:20

0 Answers0