SDK Java – SoftAfrik Push API
Client Java officiel pour utiliser SoftAfrik Push API : utilisateurs, devices, notifications push.
1. Installation
Le SDK Java est fourni comme un module autonome (.jar) ou comme une classe Java simple.
/your-project
/sdk
/java
SoftAfrikPushClient.java
README.md
examples/
BasicPush.java
Vous pouvez inclure la classe directement dans votre projet ou importer le .jar fourni.
2. Classe Java officielle
Voici la classe Java officielle à placer dans sdk/java/SoftAfrikPushClient.java.
// sdk/java/SoftAfrikPushClient.java
package sdk.java;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class SoftAfrikPushClient {
private final String apiKey;
private final String baseUrl;
public SoftAfrikPushClient(String apiKey) {
this(apiKey, "https://softafrik-push-api.brillanciel.com");
}
public SoftAfrikPushClient(String apiKey, String baseUrl) {
this.apiKey = apiKey;
this.baseUrl = baseUrl.replaceAll("/$", "");
}
// POST helper
private String post(String path, String jsonPayload) throws Exception {
URL url = new URL(baseUrl + path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("X-API-KEY", apiKey);
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
try(OutputStream os = con.getOutputStream()) {
os.write(jsonPayload.getBytes(StandardCharsets.UTF_8));
}
InputStream inputStream =
con.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST
? con.getInputStream()
: con.getErrorStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder response = new StringBuilder();
while ((line = in.readLine()) != null) {
response.append(line);
}
in.close();
return response.toString();
}
// Register user -> returns PUID
public String registerUser(String externalUserRef, String metaJson) throws Exception {
String payload = "{"
+ "\"external_user_ref\":\"" + externalUserRef + "\","
+ "\"meta\":" + metaJson
+ "}";
return post("/api/users/register", payload);
}
// Register device
public String registerDevice(
String puid,
String app,
String platform,
String token,
String uid,
String lang,
String country
) throws Exception {
String payload =
"{"
+ "\"puid\":\"" + puid + "\","
+ "\"app_name\":\"" + app + "\","
+ "\"platform\":\"" + platform + "\","
+ "\"device_token\":\"" + token + "\""
+ (uid != null ? ",\"device_uid\":\"" + uid + "\"" : "")
+ (lang != null ? ",\"lang\":\"" + lang + "\"" : "")
+ (country != null ? ",\"country\":\"" + country + "\"" : "")
+ "}";
return post("/api/devices/register", payload);
}
// Push to user
public String pushToUser(String puid, String title, String body, String dataJson) throws Exception {
String payload =
"{"
+ "\"puid\":\"" + puid + "\","
+ "\"title\":\"" + title + "\","
+ "\"body\":\"" + body + "\","
+ "\"data\":" + dataJson
+ "}";
return post("/api/push/to-user", payload);
}
// Push to device
public String pushToDevice(String token, String title, String body, String dataJson) throws Exception {
String payload =
"{"
+ "\"device_token\":\"" + token + "\","
+ "\"title\":\"" + title + "\","
+ "\"body\":\"" + body + "\","
+ "\"data\":" + dataJson
+ "}";
return post("/api/push/to-device", payload);
}
// Broadcast
public String broadcast(String app, String country, String title, String body, String dataJson) throws Exception {
String payload =
"{"
+ "\"title\":\"" + title + "\","
+ "\"body\":\"" + body + "\","
+ "\"data\":" + dataJson
+ (app != null ? ",\"app_name\":\"" + app + "\"" : "")
+ (country != null ? ",\"country\":\"" + country + "\"" : "")
+ "}";
return post("/api/push/broadcast", payload);
}
}
3. Exemple complet d’utilisation
// sdk/java/examples/BasicPush.java
import sdk.java.SoftAfrikPushClient;
public class BasicPush {
public static void main(String[] args) throws Exception {
SoftAfrikPushClient client = new SoftAfrikPushClient("VOTRE_API_KEY");
// 1) User -> PUID
String user = client.registerUser("user_12345", "{\"full_name\":\"John\",\"email\":\"john@example.com\"}");
System.out.println("USER => " + user);
// 2) Register device
String dev = client.registerDevice(
"sa_1001_xxxxxxxx",
"MyFintechApp",
"android",
"AAAAxxxxx",
"device-uuid-123",
"fr",
"TG"
);
System.out.println("DEVICE => " + dev);
// 3) Push to user
String push = client.pushToUser(
"sa_1001_xxxxxxxx",
"Transfert reçu",
"Vous avez reçu 20 000 XOF",
"{\"transaction_id\":9932}"
);
System.out.println("PUSH => " + push);
}
}
Rappel des endpoints
POST /api/users/registerPOST /api/devices/registerPOST /api/devices/unregisterPOST /api/push/to-userPOST /api/push/to-devicePOST /api/push/broadcast
Essai rapide
SoftAfrikPushClient client = new SoftAfrikPushClient("VOTRE_API_KEY");
String resp = client.pushToUser(
"sa_1001_xxxxxxxx",
"Ping",
"Hello from SoftAfrik Push API",
"{}"
);
System.out.println(resp);