Add message forwarding
This commit is contained in:
parent
0ed93a9228
commit
82095e1284
|
@ -83,19 +83,19 @@ class ApiUtils {
|
|||
override fun onComplete() {
|
||||
saveUrl(context, "${ssoAccount.url}${mApiEndpoint}")
|
||||
// Sync once it is registered
|
||||
cSync(deviceId!!)
|
||||
cSync(context, deviceId!!)
|
||||
Log.d(TAG, "mApi register: onComplete")
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// Sync directly
|
||||
Log.d(TAG, "Found deviceId: $deviceId")
|
||||
cSync(deviceId!!)
|
||||
cSync(context, deviceId!!)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun cSync(deviceId: String) {
|
||||
private fun cSync(context: Context, deviceId: String) {
|
||||
val client = OkHttpClient.Builder()
|
||||
.readTimeout(0, TimeUnit.SECONDS)
|
||||
.retryOnConnectionFailure(false)
|
||||
|
@ -107,7 +107,7 @@ class ApiUtils {
|
|||
.build()
|
||||
|
||||
factory = EventSources.createFactory(client)
|
||||
factory.newEventSource(request, SSEListener())
|
||||
factory.newEventSource(request, SSEListener(context))
|
||||
Log.d(TAG, "doConnect done.")
|
||||
}
|
||||
|
||||
|
|
|
@ -11,15 +11,17 @@ import org.unifiedpush.distributor.nextpush.account.getUrl
|
|||
|
||||
private const val TAG = "DistributorUtils"
|
||||
|
||||
fun sendMessage(context: Context, token: String, message: String){
|
||||
val application = getApp(context, token)
|
||||
fun sendMessage(context: Context, appToken: String, message: String) {
|
||||
val db = MessagingDatabase(context)
|
||||
val connectorToken = db.getConnectorToken(appToken)
|
||||
val application = getApp(context, connectorToken, db)
|
||||
if (application.isNullOrBlank()) {
|
||||
return
|
||||
}
|
||||
val broadcastIntent = Intent()
|
||||
broadcastIntent.`package` = application
|
||||
broadcastIntent.action = ACTION_MESSAGE
|
||||
broadcastIntent.putExtra(EXTRA_TOKEN, token)
|
||||
broadcastIntent.putExtra(EXTRA_TOKEN, connectorToken)
|
||||
broadcastIntent.putExtra(EXTRA_MESSAGE, message)
|
||||
context.sendBroadcast(broadcastIntent)
|
||||
}
|
||||
|
@ -49,8 +51,10 @@ fun sendUnregistered(context: Context, connectorToken: String) {
|
|||
context.sendBroadcast(broadcastIntent)
|
||||
}
|
||||
|
||||
fun getApp(context: Context, connectorToken: String): String?{
|
||||
val db = MessagingDatabase(context)
|
||||
fun getApp(context: Context,
|
||||
connectorToken: String,
|
||||
db: MessagingDatabase = MessagingDatabase(context)
|
||||
): String?{
|
||||
val app = db.getPackageName(connectorToken)
|
||||
db.close()
|
||||
return if (app.isBlank()) {
|
||||
|
|
|
@ -101,6 +101,25 @@ class MessagingDatabase(context: Context):
|
|||
}
|
||||
}
|
||||
|
||||
fun getConnectorToken(appToken: String): String {
|
||||
val db = readableDatabase
|
||||
val projection = arrayOf(FIELD_CONNECTOR_TOKEN)
|
||||
val selection = "$FIELD_APP_TOKEN = ?"
|
||||
val selectionArgs = arrayOf(appToken)
|
||||
return db.query(
|
||||
TABLE_APPS,
|
||||
projection,
|
||||
selection,
|
||||
selectionArgs,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
).use { cursor ->
|
||||
val column = cursor.getColumnIndex(FIELD_CONNECTOR_TOKEN)
|
||||
if (cursor.moveToFirst() && column >= 0) cursor.getString(column) else ""
|
||||
}
|
||||
}
|
||||
|
||||
fun listTokens(): List<String> {
|
||||
val db = readableDatabase
|
||||
val projection = arrayOf(FIELD_CONNECTOR_TOKEN)
|
||||
|
|
|
@ -1,14 +1,19 @@
|
|||
package org.unifiedpush.distributor.nextpush.services
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Base64
|
||||
import okhttp3.sse.EventSourceListener
|
||||
import okhttp3.sse.EventSource
|
||||
import android.util.Log
|
||||
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.sendMessage
|
||||
|
||||
private const val TAG = "SSEListener"
|
||||
|
||||
class SSEListener : EventSourceListener() {
|
||||
class SSEListener (val context: Context) : EventSourceListener() {
|
||||
private var pingtime = 0.toLong()
|
||||
private val networkConnected = false
|
||||
|
||||
|
@ -31,9 +36,15 @@ class SSEListener : EventSourceListener() {
|
|||
if (eventType == "ping") {
|
||||
Log.d(TAG, "SSE ping received.")
|
||||
}
|
||||
if (eventType != "notification") return
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onClosed(eventSource: EventSource) {
|
||||
|
|
Loading…
Reference in New Issue