我正在创建一个使用OneSignal发送通知的应用程序,并且必须以
JSON格式执行POST请求.
要向用户发送通知,我必须使用必须是数组的include_player_ids参数,因为可以向多个用户发送相同的通知(在我的情况下,我只向一个用户发送通知).
我使用JSONArray创建此数组,但在将其添加到JSONObject时,字段include_player_ids还有额外的引号.
是)我有的:
{
"headings": {"en":"my_title"},
"contents": {"en":"my_text"},
"include_player_ids": "[\"my-player-id\"]",
"app_id":"my-app-id"
}
如您所见,数组[]周围有一些引用.
我猜这是OneSignal的响应错误:
错误“:[”include_player_ids必须是一个数组“]
我想要的是 :
...
"include_player_ids": ["my-player-id"]
...
这很奇怪,因为将JSONObject添加到JSONObject不会这样做,即使它与标题/内容字段中看到的非常相似
我的代码:
import org.json.JSONException;
import org.json.JSONObject;
import org.json.alt.JSONArray;
JSONObject headings = new JSONObject();
JSONObject contents = new JSONObject();
JSONArray player_id = new JSONArray();
JSONObject notification = new JSONObject();
try {
notification.put("app_id", appId);
notification.put("include_player_ids", player_id);
player_id.put(idUser);
headings.put("en", "my_title");
contents.put("en", "my_text");
notification.put("headings", headings);
notification.put("contents", contents);
} catch (JSONException e) {
System.out.println("JSONException :" + e.getMessage());
}
idUser是一个String
在此先感谢您的帮助,