I am trying to make an API call to register users for push notifications.
I have read in other posts that using .execute() instead of .enqueue may work better? But when debugging, it jumped from call.execute() to the catch block.
In my interface file for API calls, jsonPlaceHolderApi:
@FormUrlEncoded
@POST("RegisterPushNotification")
Call<String> registerPushNotification(@Field("accessToken") String accessToken, @Field("deviceToken") String deviceToken, @Field("platform") String platform, @Field("phone_number") String phone_number);
In SettingsActivity, where the sendRegistrationToServer is called:
public void onClick(DialogInterface d, int which) {
if (which == -1) {
phoneNumberFromInput = input.getText().toString();
//actually make the API call to register for push notifications
MyFirebaseMessagingService firebase = new MyFirebaseMessagingService(accessToken, deviceToken, phoneNumberFromInput);
firebase.sendRegistrationToServer();
}
}
In MyFirebaseMessagingService
public void sendRegistrationToServer() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://fakelinktomyapi.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Call<String> call = jsonPlaceHolderApi.registerPushNotification(accessToken, deviceToken, "Android", phoneNumber);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()){
String message = "You're all set up to receive push notifications on this device. You may opt out at any time.";
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
else {
String message = "We're having trouble setting up push notifications for that number. Please try again.";
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.i("failure", "could not register for push");
}
});
}
Even though the phone number, access token, and device token were all NOT null (I printed their values and the debugger showed them too), once I step INTO the line "call.enqueue(new Callback)", the debugger shows this error:
phoneNumber = No such instance field: 'phoneNumber'
jsonPlaceHolderApi = No such instance field: 'jsonPlaceHolderApi'
accessToken = No such instance field: 'accessToken'
deviceToken = no such instance field 'deviceToken'