So I have this customer, and I want to update the fcm_token
value in the customer object via post request using volley, but It's not working..!
See the json link >> http://www.mocky.io/v2/5911638a1200001e020fb5d2
My attempt so far..
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
RequestQueue requestQueue = Volley.newRequestQueue(this);
HashMap<String, String> params = new HashMap<String, String>();
params.put("fcm_token", token);
Customer me = Hawk.get("user");
String URL = API_URLs.TokenURL(me.getID());
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method
.POST, URL, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Log
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//Log
}
}
);
requestQueue.add(jsObjRequest);
UPDATE
I change request to this and still didn't change..!?
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
URL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("fcm_token", token);
return params;
}
};
requestQueue.add(jsonObjReq);
UPDATE 2
so after a lot of attempts still not solved, but I'm gonna explain what I want to do exactly maybe you will get what I want to accomplish, I wanna send my device fcm token to server based on the user id when login, so I have a Customer model class that has the value of fcm_token
it must after the request is made it must be set as my token, the process happens in web server btw.
Here's my code so far..
MyFirebaseInstanceIDService Class
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
String refreshedToken;
@Override
public void onTokenRefresh() {
refreshedToken = FirebaseInstanceId.getInstance().getToken();
Hawk.put("token", refreshedToken);
}
public static void sendRegistrationToServer(Context context) {
}
}
Customer Model Class
public class Customer {
@SerializedName("avatar_url")
@Expose
private String cusPicURL;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
@SerializedName("fcm_token")
@Expose
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
// ..... more variables & setters and getters
My Request Code inside a repository class (all request are here..)
public static void UpdateFCM_Token(final Context context, final String token) {
RequestQueue requestQueue = Volley.newRequestQueue(context);
Customer me = Hawk.get("user");
Log.i("USER ID: ", "" + me.getID());
String URL = API_URLs.TokenURL(me.getID());
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST,
URL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject o = response.getJSONObject("customer");
Gson gson = new Gson();
Customer customer = gson.fromJson(o.toString(), Customer.class);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("FCM OnResponse", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("FCM Error: ", "" + error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("fcm_token", token);
return params;
}
};
requestQueue.add(req);
}
The fragment in MainActivity
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// code..
String token = Hawk.get("token");
Log.i("TOKEN", token);
DriverRepository.UpdateFCM_Token(getActivity(), token);
return rootView;
}
The Logs