package org.swissapp.pushs import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import org.json.JSONObject import java.util.TimeZone import java.util.concurrent.TimeUnit /** * Swissapp Pushs — client Android (Kotlin) minimal * 1) Récupère le token FCM via FirebaseMessaging.getInstance().token * 2) Appelle register(pushToken = …) * * CDN: https://sdk.pushs.swissapp.net/native/android/SwissappPushs.kt * * Dépendances Gradle : * implementation("com.squareup.okhttp3:okhttp:4.12.0") * implementation(platform("com.google.firebase:firebase-bom:33.1.0")) * implementation("com.google.firebase:firebase-messaging") */ class SwissappPushs( private val apiKey: String, private val apiUrl: String = "https://api.pushs.swissapp.net", var externalUserId: String? = null ) { companion object { const val VERSION = "1.0.0" } private val client = OkHttpClient.Builder() .connectTimeout(20, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .build() private val jsonMedia = "application/json; charset=utf-8".toMediaType() fun register( pushToken: String, platform: String = "android", deviceName: String? = null, deviceModel: String? = android.os.Build.MODEL, appVersion: String? = null, externalUserId: String? = null ): JSONObject { val body = JSONObject() .put("push_token", pushToken) .put("platform", platform) .put("locale", java.util.Locale.getDefault().toLanguageTag()) .put("timezone", TimeZone.getDefault().id) val ext = externalUserId ?: this.externalUserId if (ext != null) body.put("external_user_id", ext) if (deviceName != null) body.put("device_name", deviceName) if (deviceModel != null) body.put("device_model", deviceModel) if (appVersion != null) body.put("app_version", appVersion) return post("/api/devices/register", body) } fun send( message: String, title: String? = null, externalUserId: String? = null, data: JSONObject? = null ): JSONObject { val body = JSONObject().put("message", message) if (title != null) body.put("title", title) if (data != null) body.put("data", data) val ext = externalUserId ?: this.externalUserId if (ext != null) body.put("external_user_id", ext) return post("/api/send", body) } private fun post(path: String, body: JSONObject): JSONObject { val req = Request.Builder() .url(apiUrl.trimEnd('/') + path) .addHeader("Authorization", "Bearer $apiKey") .addHeader("Content-Type", "application/json") .addHeader("X-Swissapp-SDK", "android/$VERSION") .post(body.toString().toRequestBody(jsonMedia)) .build() client.newCall(req).execute().use { resp -> val text = resp.body?.string().orEmpty() val json = try { JSONObject(text) } catch (_: Exception) { JSONObject() } if (!resp.isSuccessful || json.optBoolean("success", true).not() && json.has("error")) { throw IllegalStateException(json.optString("error", "HTTP ${resp.code}")) } return json } } } /* // Dans votre Application / Activity après login : FirebaseMessaging.getInstance().token.addOnSuccessListener { token -> Thread { try { val push = SwissappPushs(apiKey = "pushs_…", externalUserId = "user_123") push.register(pushToken = token) } catch (e: Exception) { Log.e("SwissappPushs", "register failed", e) } }.start() } // Firebase Console → Project settings → Service accounts → Generate new private key // → coller le JSON dans my.swissapp.org → Pushs → Configuration Android (FCM) */