Firebase 从 android 应用程序发送 http 云消息
Firebase send http cloud messaging from android app
我目前正在使用 http post 请求从 Postman 发送 firebase 云消息。一切正常。我现在正在尝试制作一个简单的 Android 应用程序来发送来自 .但是它不起作用,我得到 InvalidRegistration 或 MissingRegistration 或 com.android.volley.AuthFailureError
在 postman 中,我向 https://fcm.googleapis.com/fcm/send
发送了一个 http post 请求
有 2 个 headers:
Key: 'Authorization' Value: 'key=AAAAnfdQ2jM:AP....'
Key: 'Content-Type' Value: application/json
然后在 body 中,使用 raw:
{
"to": "/topics/anytopic",
"data": {
"my_message": "Hi everyone!",
}
}
在哪里以及如何将此信息放入我的 volley http post 请求中?
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);
我找不到任何这样的例子。
非常感谢。
您需要像这样添加 JSON 正文:
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);
我目前正在使用 http post 请求从 Postman 发送 firebase 云消息。一切正常。我现在正在尝试制作一个简单的 Android 应用程序来发送来自 .但是它不起作用,我得到 InvalidRegistration 或 MissingRegistration 或 com.android.volley.AuthFailureError
在 postman 中,我向 https://fcm.googleapis.com/fcm/send
发送了一个 http post 请求有 2 个 headers:
Key: 'Authorization' Value: 'key=AAAAnfdQ2jM:AP....'
Key: 'Content-Type' Value: application/json
然后在 body 中,使用 raw:
{
"to": "/topics/anytopic",
"data": {
"my_message": "Hi everyone!",
}
}
在哪里以及如何将此信息放入我的 volley http post 请求中?
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);
我找不到任何这样的例子。
非常感谢。
您需要像这样添加 JSON 正文:
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);