2026-04-28 09:45:20 +08:00
|
|
|
package com.xuqm.im.service;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.client.RestClientException;
|
|
|
|
|
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.http.HttpClient;
|
|
|
|
|
import java.net.http.HttpRequest;
|
|
|
|
|
import java.net.http.HttpResponse;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
public class ImPushBridgeClient {
|
|
|
|
|
|
|
|
|
|
private final HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
2026-04-28 16:08:07 +08:00
|
|
|
@Value("${im.push-service-url:http://192.168.116.9:8083}")
|
2026-04-28 09:45:20 +08:00
|
|
|
private String pushServiceUrl;
|
|
|
|
|
|
|
|
|
|
@Value("${im.internal-token:xuqm-internal-token}")
|
|
|
|
|
private String internalToken;
|
|
|
|
|
|
|
|
|
|
public ImPushBridgeClient(ObjectMapper objectMapper) {
|
|
|
|
|
this.objectMapper = objectMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void notifyUsers(String appId, List<String> userIds, String title, String body, String payload) {
|
|
|
|
|
if (userIds == null || userIds.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
Map<String, Object> bodyMap = new LinkedHashMap<>();
|
|
|
|
|
bodyMap.put("appId", appId);
|
|
|
|
|
bodyMap.put("userIds", userIds);
|
|
|
|
|
bodyMap.put("title", title);
|
|
|
|
|
bodyMap.put("body", body);
|
|
|
|
|
bodyMap.put("payload", payload);
|
|
|
|
|
String json = objectMapper.writeValueAsString(bodyMap);
|
|
|
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
|
.uri(URI.create(pushServiceUrl + "/api/push/internal/notify"))
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
|
.header("X-Internal-Token", internalToken)
|
|
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(json))
|
|
|
|
|
.build();
|
|
|
|
|
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|