【发布时间】:2017-11-02 18:27:21
【问题描述】:
我想使用 Volley 向 FCM 发送通知。 该方法期望获取“数据”下的消息信息。 问题是当我在 volley 中使用“put”时 - 就像这里:
JSONObject notificationTitleObject = new JSONObject().put("message_title",notificationTitle);
上面的行从“数据”中删除。 我曾尝试使用 JsonArray,然后将其作为“数据”的值。 没用。
如果我在 Json 中编写理想的结果,它将如下所示:
{ "data": {
"message_title": "XXXX",
"message": "XXXXX"
"message_image_url": "XXX"
},
"to" : "/topics/Notifications_For_Event_Items"
}
完整代码在这里:
private void sendNotifToServer() throws JSONException {
rootObject = new JSONObject();
JSONObject notificationTitleObject = new JSONObject().put("message_title",notificationTitle);
JSONObject notificationMessageObject = new JSONObject().put("message",itemName.getText().toString());
JSONObject notificationImageObject = new JSONObject().put("message_image_url",imageUrl);
try {
rootObject.put("to","/topics/Notifications_For_Event_Items");
rootObject.put("data",notificationTitleObject);
rootObject.put("data",notificationMessageObject);
rootObject.put("data",notificationImageObject);
// rootObject.put("data",new JSONObject().put("fragmentType","1"));
}catch (JSONException e){
e.printStackTrace();
}
RequestQueue queue = Volley.newRequestQueue(AddEventItemsActivity.this);
StringRequest request = new StringRequest(Request.Method.POST, notificationUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
return rootObject.toString().getBytes();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String,String>();
headers.put("Content-Type","application/json");
headers.put("Authorization","key="+notifApiKey);
return headers;
}
};
【问题讨论】:
标签: android json post android-volley firebase-cloud-messaging