How to Send AlertOver Push Notifications with Java HttpClient – Full Code Example
This article provides a complete Java HttpClient implementation for sending push notifications through AlertOver’s API, including JSON payload construction, request execution, response handling, and highlights potential integration tips for production environments.
AlertOver is a push‑notification service that offers a simple HTTP API, but its official documentation lacks a Java example. The following guide shows how to build a Java method that posts a JSON payload to the AlertOver endpoint using Apache HttpClient.
public static void sendMessageToMobile(String title, String content, String receiver) throws JSONException, ClientProtocolException, IOException {
String source = "s-6bf44a17-73ba-45dc-9443-c34c5d53"; // AlertOver source ID
if (title == null) {
title = "测试"; // default title
}
if (content == null) {
content = "我是008!"; // default content
}
// Convert to ISO‑8859‑1 to match API expectations
title = new String(title.getBytes(), "ISO-8859-1");
content = new String(content.getBytes(), "ISO-8859-1");
CloseableHttpClient httpClients = HttpClients.createDefault(); // create client
JSONObject jsonObject = new JSONObject();
jsonObject.put("source", source.trim());
jsonObject.put("receiver", receiver.trim());
jsonObject.put("content", content.trim());
jsonObject.put("title", title.trim());
HttpPost httpPost = new HttpPost("https://api.alertover.com/v1/alert"); // API endpoint
StringEntity entity = new StringEntity(jsonObject.toString());
entity.setContentEncoding("ISO-8859-1");
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse res = httpClients.execute(httpPost);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
output("测试发送消息成功!");
} else {
HttpEntity httpEntity = res.getEntity();
output(httpEntity.toString());
}
httpClients.close(); // release resources
}The method performs the following steps:
Define the AlertOver source identifier.
Provide default values for title and content when they are null.
Convert title and content to ISO-8859-1 encoding as required by the API.
Construct a JSONObject containing source, receiver, title, and content.
Create an HttpPost request to https://api.alertover.com/v1/alert and attach the JSON payload as a StringEntity with the appropriate content type.
Execute the request with a CloseableHttpClient and check the HTTP status code.
Print a success message if the response status is 200 OK; otherwise, output the raw response entity for debugging.
Close the HTTP client to free resources.
Important note: This snippet demonstrates a single‑request usage of HttpClient and is intended for demonstration purposes only. In a real‑world notification module you should consider using a connection pool, asynchronous execution, and proper error handling to suit the needs of your project.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
