Add message forwarding

This commit is contained in:
S1m 2021-11-23 01:41:05 +01:00
parent 0ed93a9228
commit 82095e1284
4 changed files with 45 additions and 11 deletions

View File

@ -83,19 +83,19 @@ class ApiUtils {
override fun onComplete() { override fun onComplete() {
saveUrl(context, "${ssoAccount.url}${mApiEndpoint}") saveUrl(context, "${ssoAccount.url}${mApiEndpoint}")
// Sync once it is registered // Sync once it is registered
cSync(deviceId!!) cSync(context, deviceId!!)
Log.d(TAG, "mApi register: onComplete") Log.d(TAG, "mApi register: onComplete")
} }
}) })
} else { } else {
// Sync directly // Sync directly
Log.d(TAG, "Found deviceId: $deviceId") 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() val client = OkHttpClient.Builder()
.readTimeout(0, TimeUnit.SECONDS) .readTimeout(0, TimeUnit.SECONDS)
.retryOnConnectionFailure(false) .retryOnConnectionFailure(false)
@ -107,7 +107,7 @@ class ApiUtils {
.build() .build()
factory = EventSources.createFactory(client) factory = EventSources.createFactory(client)
factory.newEventSource(request, SSEListener()) factory.newEventSource(request, SSEListener(context))
Log.d(TAG, "doConnect done.") Log.d(TAG, "doConnect done.")
} }

View File

@ -11,15 +11,17 @@ import org.unifiedpush.distributor.nextpush.account.getUrl
private const val TAG = "DistributorUtils" private const val TAG = "DistributorUtils"
fun sendMessage(context: Context, token: String, message: String){ fun sendMessage(context: Context, appToken: String, message: String) {
val application = getApp(context, token) val db = MessagingDatabase(context)
val connectorToken = db.getConnectorToken(appToken)
val application = getApp(context, connectorToken, db)
if (application.isNullOrBlank()) { if (application.isNullOrBlank()) {
return return
} }
val broadcastIntent = Intent() val broadcastIntent = Intent()
broadcastIntent.`package` = application broadcastIntent.`package` = application
broadcastIntent.action = ACTION_MESSAGE broadcastIntent.action = ACTION_MESSAGE
broadcastIntent.putExtra(EXTRA_TOKEN, token) broadcastIntent.putExtra(EXTRA_TOKEN, connectorToken)
broadcastIntent.putExtra(EXTRA_MESSAGE, message) broadcastIntent.putExtra(EXTRA_MESSAGE, message)
context.sendBroadcast(broadcastIntent) context.sendBroadcast(broadcastIntent)
} }
@ -49,8 +51,10 @@ fun sendUnregistered(context: Context, connectorToken: String) {
context.sendBroadcast(broadcastIntent) context.sendBroadcast(broadcastIntent)
} }
fun getApp(context: Context, connectorToken: String): String?{ fun getApp(context: Context,
val db = MessagingDatabase(context) connectorToken: String,
db: MessagingDatabase = MessagingDatabase(context)
): String?{
val app = db.getPackageName(connectorToken) val app = db.getPackageName(connectorToken)
db.close() db.close()
return if (app.isBlank()) { return if (app.isBlank()) {

View File

@ -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> { fun listTokens(): List<String> {
val db = readableDatabase val db = readableDatabase
val projection = arrayOf(FIELD_CONNECTOR_TOKEN) val projection = arrayOf(FIELD_CONNECTOR_TOKEN)

View File

@ -1,14 +1,19 @@
package org.unifiedpush.distributor.nextpush.services package org.unifiedpush.distributor.nextpush.services
import android.content.Context
import android.util.Base64
import okhttp3.sse.EventSourceListener import okhttp3.sse.EventSourceListener
import okhttp3.sse.EventSource import okhttp3.sse.EventSource
import android.util.Log import android.util.Log
import okhttp3.Response import okhttp3.Response
import java.lang.Exception 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" private const val TAG = "SSEListener"
class SSEListener : EventSourceListener() { class SSEListener (val context: Context) : EventSourceListener() {
private var pingtime = 0.toLong() private var pingtime = 0.toLong()
private val networkConnected = false private val networkConnected = false
@ -31,9 +36,15 @@ class SSEListener : EventSourceListener() {
if (eventType == "ping") { if (eventType == "ping") {
Log.d(TAG, "SSE ping received.") Log.d(TAG, "SSE ping received.")
} }
if (eventType != "notification") return if (eventType != "message") return
Log.d(TAG, "Notification event received.") Log.d(TAG, "Notification event received.")
// handle notification // 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) { override fun onClosed(eventSource: EventSource) {