Add deleteApp SSE event

This commit is contained in:
S1m 2021-11-24 01:10:50 +01:00
parent 85ed92357f
commit 85b838acfd

View File

@ -9,7 +9,9 @@ import okhttp3.Response
import java.lang.Exception import java.lang.Exception
import com.google.gson.Gson import com.google.gson.Gson
import org.unifiedpush.distributor.nextpush.api.SSEResponse 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.sendMessage
import org.unifiedpush.distributor.nextpush.distributor.sendUnregistered
private const val TAG = "SSEListener" 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) { override fun onEvent(eventSource: EventSource, id: String?, eventType: String?, data: String) {
Log.d(TAG, "New SSE message event=$eventType message=$data") Log.d(TAG, "New SSE message event=$eventType message=$data")
if (eventType == "warning") { when (eventType) {
Log.d(TAG, "Warning event received.") "warning" -> Log.d(TAG, "Warning event received.")
// Notification warning "ping" -> Log.d(TAG, "SSE ping received.")
} "message" -> {
if (eventType == "ping") {
Log.d(TAG, "SSE ping received.")
}
if (eventType != "message") return
Log.d(TAG, "Notification event received.") Log.d(TAG, "Notification event received.")
// handle notification
val message = Gson().fromJson(data, SSEResponse::class.java) val message = Gson().fromJson(data, SSEResponse::class.java)
if ( message.type == "message" ) { sendMessage(
sendMessage(context, context,
message.token, message.token,
String(Base64.decode(message.message,Base64.DEFAULT))) 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) { override fun onClosed(eventSource: EventSource) {
Log.d(TAG, "onClosed: $eventSource") Log.d(TAG, "onClosed: $eventSource")
isServiceStarted = false
createWarningNotification(context)
startListener(context) startListener(context)
} }
override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) { override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) {
isServiceStarted = false
createWarningNotification(context) createWarningNotification(context)
t?.let { t?.let {
Log.d(TAG, "An error occurred: $t") Log.d(TAG, "An error occurred: $t")