SDK C# – SoftAfrik Push API
Client C# simple pour consommer SoftAfrik Push API (REST, JSON, push notifications).
1. Installation
Le SDK C# est une classe autonome utilisable dans tout projet .NET.
/your-dotnet-project
/SoftAfrik
SoftAfrikPushClient.cs
Program.cs
2. Dépendances requises
dotnet add package Newtonsoft.Json
3. Classe SoftAfrikPushClient.cs
Copiez ce code dans SoftAfrikPushClient.cs.
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
public class SoftAfrikPushClient
{
private readonly string apiKey;
private readonly string baseUrl;
private readonly HttpClient http;
public SoftAfrikPushClient(string apiKey,
string baseUrl = "https://softafrik-push-api.brillanciel.com")
{
this.apiKey = apiKey;
this.baseUrl = baseUrl.TrimEnd('/');
this.http = new HttpClient();
}
private async Task<T> Post<T>(string path, object payload)
{
var url = baseUrl + path;
var json = JsonConvert.SerializeObject(payload);
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Add("X-API-KEY", apiKey);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await http.SendAsync(request);
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
public Task<dynamic> RegisterUser(string refId, object meta)
=> Post<dynamic>("/api/users/register",
new { external_user_ref = refId, meta });
public Task<dynamic> RegisterDevice(object payload)
=> Post<dynamic>("/api/devices/register", payload);
public Task<dynamic> UnregisterDevice(string token)
=> Post<dynamic>("/api/devices/unregister",
new { device_token = token });
public Task<dynamic> PushToUser(string puid, string title, string body, object data)
=> Post<dynamic>("/api/push/to-user",
new { puid, title, body, data });
public Task<dynamic> PushToDevice(string token, string title, string body, object data)
=> Post<dynamic>("/api/push/to-device",
new { device_token = token, title, body, data });
public Task<dynamic> Broadcast(object payload)
=> Post<dynamic>("/api/push/broadcast", payload);
}
4. Exemple complet (Program.cs)
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var client = new SoftAfrikPushClient("VOTRE_API_KEY_ICI");
// 1. Register user => get PUID
var user = await client.RegisterUser("user_12345", new {
full_name = "John Doe",
email = "john@example.com"
});
string puid = user.puid;
Console.WriteLine("PUID = " + puid);
// 2. Push notification
var resp = await client.PushToUser(
puid,
"Transfert reçu",
"Vous avez reçu 20 000 XOF",
new { transaction_id = 9932 }
);
Console.WriteLine(resp);
}
}
Endpoints
POST /api/users/registerPOST /api/devices/registerPOST /api/devices/unregisterPOST /api/push/to-userPOST /api/push/to-devicePOST /api/push/broadcast
var client = new SoftAfrikPushClient("API_KEY");
await client.PushToUser(
"sa_1001_xxxxxxxx",
"Ping",
"Hello from SoftAfrik Push API",
new { ping = true }
);