diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt index ae762e9..d7d1b02 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt @@ -9,7 +9,9 @@ import okhttp3.Response import java.lang.Exception import com.google.gson.Gson import org.unifiedpush.distributor.nextpush.api.SSEResponse +import org.unifiedpush.distributor.nextpush.distributor.getDb import org.unifiedpush.distributor.nextpush.distributor.sendMessage +import org.unifiedpush.distributor.nextpush.distributor.sendUnregistered private const val TAG = "SSEListener" @@ -25,30 +27,39 @@ class SSEListener (val context: Context) : EventSourceListener() { override fun onEvent(eventSource: EventSource, id: String?, eventType: String?, data: String) { Log.d(TAG, "New SSE message event=$eventType message=$data") - if (eventType == "warning") { - Log.d(TAG, "Warning event received.") - // Notification warning - } - if (eventType == "ping") { - Log.d(TAG, "SSE ping received.") - } - if (eventType != "message") return - Log.d(TAG, "Notification event received.") - // handle notification - val message = Gson().fromJson(data, SSEResponse::class.java) - if ( message.type == "message" ) { - sendMessage(context, - message.token, - String(Base64.decode(message.message,Base64.DEFAULT))) + when (eventType) { + "warning" -> Log.d(TAG, "Warning event received.") + "ping" -> Log.d(TAG, "SSE ping received.") + "message" -> { + Log.d(TAG, "Notification event received.") + val message = Gson().fromJson(data, SSEResponse::class.java) + sendMessage( + context, + message.token, + String(Base64.decode(message.message, Base64.DEFAULT)) + ) + } + "deleteApp" -> { + val message = Gson().fromJson(data, SSEResponse::class.java) + val db = getDb(context.applicationContext) + val connectorToken = db.getConnectorToken(message.token) + if (connectorToken.isEmpty()) + return + sendUnregistered(context.applicationContext, connectorToken) + db.unregisterApp(connectorToken) + } } } override fun onClosed(eventSource: EventSource) { Log.d(TAG, "onClosed: $eventSource") + isServiceStarted = false + createWarningNotification(context) startListener(context) } override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) { + isServiceStarted = false createWarningNotification(context) t?.let { Log.d(TAG, "An error occurred: $t")