I am currently sending firebase cloud messages from Postman, using an http post request. Its all working fine. I am now trying to make a simple Android app to send the messages from, . However it doesn't work and I get either InvalidRegistration or MissingRegistration or com.android.volley.AuthFailureError
In postman, I do an http post request to https://fcm.googleapis.com/fcm/send
With 2 headers:
Key: 'Authorization' Value: 'key=AAAAnfdQ2jM:AP....'
Key: 'Content-Type' Value: application/json
Then in body, using raw:
{
"to": "/topics/anytopic",
"data": {
"my_message": "Hi everyone!",
}
}
Where and how do I put this information in my volley http post request?
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest sr = new StringRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Response: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
return params;
}
};
queue.add(sr);
I can't find any example of this.
Thanks so much.
You need to add a JSON body like this:
JSONObject jsonBody = new JSONObject();
jsonBody.put("to", REUVEN_TOKEN_BE_YOUNG);
JSONObject jsonObject = new JSONObject();
jsonObject.put("my_message", "Keep eating healthy!");
jsonBody.putOpt("data", jsonObject);
final String requestBody = jsonBody.toString();
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest sr = new StringRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Response: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "That didn't work..." + error);
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Authorization",BE_YOUNG_APP_KEY);
return params;
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
};
queue.add(sr);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加