5

I'm making an application to send notifications using OneSignal and must do a POST request in JSON format.

To send notification to a user I have to use the include_player_ids argument which have to be an array because it is possible to send to multiple users the same notification (in my case I only send notification to one user). I use a JSONArray to create this array but when adding it to the JSONObject, there are extra quotes for the field include_player_ids.

What I have:

{
  "headings": {"en":"my_title"},
  "contents": {"en":"my_text"},
  "include_player_ids": "[\"my-player-id\"]",
  "app_id":"my-app-id"
}

As you can see, there is some quote around the array [ ].

I guess it's what is making the response error from OneSignal : errors":["include_player_ids must be an array"]

What I want :

...
"include_player_ids": ["my-player-id"] 
...

It is weird because adding JSONObject to JSONObject doesn't do this even though it is quite similar as you can see with the headings / contents fields

My code :

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 is a String

Thanks in advance for any help,

2
  • The result of notification.toString() have quotes at the array? Also, i think you should put the idUser at player_id BEFORE adding to the object Commented May 11, 2018 at 15:46
  • @MarcosVasconcelos yes notification.toString() have quotes. The order is weird because I was following some tutorials that did that way. Indeed it makes more sense to put the idUser before. Thanks for the reply Commented May 14, 2018 at 7:25

1 Answer 1

11

I believe the problem is that you're using org.json.alt.JSONArray instead of org.json.JSONArray. I'm not familiar with that class, but I suspect JSONObject.put is just calling toString() on it rather than treating it as an existing JSON array. Here's a short but complete example that doesn't have the problem:

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray; // Note the import here

public class Test {
    public static void main(String[] args) throws JSONException {
        JSONArray playerIds = new JSONArray();
        playerIds.put("a");
        playerIds.put("b");
        JSONObject notification = new JSONObject();
        notification.put("include_player_ids", playerIds);
        System.out.println(notification);
      }
}

Output:

{"include_player_ids":["a","b"]}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.