Issue
I would like to send a notification from a device to the other device but it keep on displaying the error message AuthFailureError. Is anyone know why how to solve this problem?
Error Message E/Volley: [18672] NetworkUtility.shouldRetryException: Unexpected response code 401 for https://fcm.googleapis.com/fcm/send
E/Volley: [18672] NetworkUtility.shouldRetryException: Unexpected response code 401 for https://fcm.googleapis.com/fcm/send
E/Result: com.android.volley.AuthFailureError
public static void SendNotifications(Context applicationContext, String targetToken, String title, String body) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
RequestQueue queue = Volley.newRequestQueue(applicationContext);
try {
JSONObject json = new JSONObject();
json.put("to", targetToken);
JSONObject notification = new JSONObject();
notification.put("title", title);
notification.put("body", body);
json.put("notification", notification);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, postUrl, json, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("Result", String.valueOf(response));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Result", String.valueOf(error));
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("Content-type", "application/json");
header.put("authorization", "key=" + fcmServerKey);
return header;
}
};
queue.add(jsonObjectRequest);
} catch (JSONException jsonException) {
jsonException.printStackTrace();
}
}
Solution
because your write
header.put("Content-type", "application/json");
header.put("authorization", "key=" + fcmServerKey);
Modify it to be
header.put("Content-Type", "application/json");
header.put("Authorization", "key=" + fcmServerKey);
Content-Type not Content-type and Authorization not authorization
Answered By - Abanoob Samy
Answer Checked By - David Marino (JavaFixing Volunteer)