Use saved keepalive
And fix restart worker
This commit is contained in:
parent
aa025da6b8
commit
cfa987c0b5
@ -3,5 +3,6 @@ package org.unifiedpush.distributor.nextpush.api
|
||||
data class SSEResponse (
|
||||
val type: String = "",
|
||||
val token: String = "",
|
||||
val message: String = ""
|
||||
val message: String = "",
|
||||
val keepalive: Int = 900
|
||||
)
|
@ -40,6 +40,7 @@ object DistributorUtils {
|
||||
broadcastIntent.putExtra(EXTRA_MESSAGE, String(message))
|
||||
broadcastIntent.putExtra(EXTRA_BYTES_MESSAGE, message)
|
||||
context.sendBroadcast(broadcastIntent)
|
||||
Log.d(TAG, "Message forwarded")
|
||||
}
|
||||
|
||||
fun sendEndpoint(context: Context, connectorToken: String) {
|
||||
|
@ -3,6 +3,7 @@ package org.unifiedpush.distributor.nextpush.services
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.work.*
|
||||
import org.unifiedpush.distributor.nextpush.services.SSEListener.Companion.keepalive
|
||||
import org.unifiedpush.distributor.nextpush.services.SSEListener.Companion.lastEventDate
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
@ -31,9 +32,13 @@ class RestartWorker (ctx: Context, params: WorkerParameters) : Worker(ctx, param
|
||||
override fun doWork(): Result {
|
||||
Log.d(TAG, "Working")
|
||||
val currentDate = Calendar.getInstance()
|
||||
val restartDate = lastEventDate
|
||||
restartDate?.add(Calendar.MINUTE, 15)
|
||||
if (restartDate == null || currentDate.after(restartDate)) {
|
||||
val restartDate = Calendar.getInstance()
|
||||
lastEventDate?.let {
|
||||
restartDate.time = it.time
|
||||
restartDate.add(Calendar.SECOND, keepalive)
|
||||
}
|
||||
Log.d(TAG, "restartDate: ${restartDate.time}")
|
||||
if (lastEventDate == null || currentDate.after(restartDate)) {
|
||||
Log.d(TAG, "Restarting")
|
||||
StartService.startListener(applicationContext)
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ class SSEListener (val context: Context) : EventSourceListener() {
|
||||
|
||||
companion object {
|
||||
var lastEventDate : Calendar? = null
|
||||
var keepalive = 900
|
||||
}
|
||||
|
||||
override fun onOpen(eventSource: EventSource, response: Response) {
|
||||
@ -40,15 +41,17 @@ class SSEListener (val context: Context) : EventSourceListener() {
|
||||
}
|
||||
|
||||
override fun onEvent(eventSource: EventSource, id: String?, type: String?, data: String) {
|
||||
Log.d(TAG, "New SSE message event=$type message=$data")
|
||||
Log.d(TAG, "New SSE message event: $type")
|
||||
StartService.wakeLock?.acquire(30000L /*30 secs*/)
|
||||
lastEventDate = Calendar.getInstance()
|
||||
|
||||
when (type) {
|
||||
"warning" -> Log.d(TAG, "Warning event received.")
|
||||
"ping" -> Log.d(TAG, "SSE ping received.")
|
||||
"keepalive" -> {
|
||||
val message = Gson().fromJson(data, SSEResponse::class.java)
|
||||
keepalive = message.keepalive
|
||||
Log.d(TAG, "New keepalive: $keepalive")
|
||||
}
|
||||
"message" -> {
|
||||
Log.d(TAG, "Notification event received.")
|
||||
val message = Gson().fromJson(data, SSEResponse::class.java)
|
||||
sendMessage(
|
||||
context,
|
||||
|
Loading…
x
Reference in New Issue
Block a user