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 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
when (eventType) {
"warning" -> Log.d(TAG, "Warning event received.")
"ping" -> Log.d(TAG, "SSE ping received.")
"message" -> {
Log.d(TAG, "Notification event received.")
// handle notification
val message = Gson().fromJson(data, SSEResponse::class.java)
if ( message.type == "message" ) {
sendMessage(context,
sendMessage(
context,
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) {
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")