Добрый день.
Создаю телеграмм бот, одной из задач которого, является отправка сообщения на сайт OneSignal, после чего с сайта уходят пуш-уведомления всем участникам бота. Я создал класс
PushNotification
public class PushNotification implements Push {
public static final String REST_API_KEY = "***************";
public static final String APP_ID = "***************";
@Override
public void sentPush(String message) {
try{
String jsonResponse;
URL url = new URL("https://onesignal.com/api/v1/notifications");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Authorization", "Basic " + REST_API_KEY);
connection.setRequestMethod("POST");
String strJsonBody = "{"
+ "\"app_id\": \"" + APP_ID + "\","
+ "\"included_segments\": [\"All\"],"
+ "\"data\": {\"foo\": \"bar\"},"
+ "\"contents\": {\"en\": \""+ message +"\"}"
+ "}";
System.out.println("strJsonBody:\n" + strJsonBody);
byte[] sendBytes = strJsonBody.getBytes(StandardCharsets.UTF_8);
connection.setFixedLengthStreamingMode(sendBytes.length);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(sendBytes);
int httpResponse = connection.getResponseCode();
System.out.println("httpResponse: " + httpResponse);
jsonResponse = mountResponseRequest(connection,httpResponse);
System.out.println("jsonResponse:\n" + jsonResponse);
}catch (Throwable t) {
t.printStackTrace();
}
}
private String mountResponseRequest(HttpsURLConnection connection, int httpResponse) throws IOException {
String jsonResponse;
Scanner scanner;
if (httpResponse >= HttpsURLConnection.HTTP_OK && httpResponse < HttpsURLConnection.HTTP_BAD_REQUEST) {
scanner = new Scanner(connection.getInputStream(), StandardCharsets.UTF_8);
}
else {
scanner = new Scanner(connection.getErrorStream(), StandardCharsets.UTF_8);
}
jsonResponse = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
scanner.close();
return jsonResponse;
}
}
После нажатия кнопки в боте запрос уходит, но получаю ошибку
strJsonBody:
{"app_id": "*****************","included_segments": ["All"],"data": {"foo": "bar"},"contents": {"en": "delete"}}
httpResponse: 400
jsonResponse:
{"errors":["Invalid app_id format"]}
Значение
app_id
указано без ошибки.
Подскажите как можно решить эту проблему ?