Parse pushData in the push receiver

This commit is contained in:
sim 2022-09-08 16:25:48 +02:00
parent bc5309b5d7
commit 3e12907b26
4 changed files with 28 additions and 22 deletions

View file

@ -22,11 +22,11 @@ import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.pushers.FcmHelper
import im.vector.app.core.pushers.PushParser
import im.vector.app.core.pushers.PushersManager
import im.vector.app.core.pushers.UnifiedPushHelper
import im.vector.app.core.pushers.VectorPushHandler
import im.vector.app.features.settings.VectorPreferences
import org.json.JSONObject
import timber.log.Timber
import javax.inject.Inject
@ -36,6 +36,7 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
@Inject lateinit var pushersManager: PushersManager
@Inject lateinit var pushParser: PushParser
@Inject lateinit var vectorPushHandler: VectorPushHandler
@Inject lateinit var unifiedPushHelper: UnifiedPushHelper
@ -53,6 +54,8 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) {
Timber.d("New Firebase message")
vectorPushHandler.onMessage(JSONObject(message.data as Map<*, *>).toString())
pushParser.parsePushDataFcm(message.data)?.let {
vectorPushHandler.handle(it)
}
}
}

View file

@ -20,6 +20,7 @@ import im.vector.app.core.pushers.model.PushData
import im.vector.app.core.pushers.model.PushDataFcm
import im.vector.app.core.pushers.model.PushDataUnifiedPush
import im.vector.app.core.pushers.model.toPushData
import org.json.JSONObject
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.util.MatrixJsonParser
import javax.inject.Inject
@ -40,12 +41,15 @@ class PushParser @Inject constructor() {
* [3] https://spec.matrix.org/latest/push-gateway-api/
* [4] https://github.com/p1gp1g/sygnal/blob/unifiedpush/sygnal/upfcmpushkin.py (Not tested for a while)
*/
fun parseData(message: String, firebaseFormat: Boolean): PushData? {
val moshi = MatrixJsonParser.getMoshi()
return if (firebaseFormat) {
tryOrNull { moshi.adapter(PushDataFcm::class.java).fromJson(message) }?.toPushData()
} else {
tryOrNull { moshi.adapter(PushDataUnifiedPush::class.java).fromJson(message) }?.toPushData()
fun parsePushDataUnifiedPush(message: ByteArray): PushData? {
return MatrixJsonParser.getMoshi().let {
tryOrNull { it.adapter(PushDataUnifiedPush::class.java).fromJson(String(message)) }?.toPushData()
}
}
fun parsePushDataFcm(message: Map<*, *>): PushData? {
return MatrixJsonParser.getMoshi().let {
tryOrNull { it.adapter(PushDataFcm::class.java).fromJson(JSONObject(message).toString()) }?.toPushData()
}
}
}

View file

@ -55,8 +55,6 @@ class VectorPushHandler @Inject constructor(
private val vectorPreferences: VectorPreferences,
private val vectorDataStore: VectorDataStore,
private val wifiDetector: WifiDetector,
private val unifiedPushHelper: UnifiedPushHelper,
private val pushParser: PushParser,
private val actionIds: NotificationActionIds,
private val context: Context,
private val buildMeta: BuildMeta
@ -74,20 +72,17 @@ class VectorPushHandler @Inject constructor(
*
* @param message the message
*/
fun onMessage(message: String) {
Timber.tag(loggerTag.value).d("## onMessage() received")
fun handle(pushData: PushData) {
Timber.tag(loggerTag.value).d("## handling pushData")
if (buildMeta.lowPrivacyLoggingEnabled) {
Timber.tag(loggerTag.value).d("## onMessage() $message")
Timber.tag(loggerTag.value).d("## pushData: $pushData")
}
runBlocking {
vectorDataStore.incrementPushCounter()
}
val pushData = pushParser.parseData(message, unifiedPushHelper.isEmbeddedDistributor())
?: return Unit.also { Timber.tag(loggerTag.value).w("Invalid received data Json format") }
// Diagnostic Push
if (pushData.eventId == PushersManager.TEST_EVENT_ID) {
val intent = Intent(actionIds.push)
@ -105,7 +100,7 @@ class VectorPushHandler @Inject constructor(
// we are in foreground, let the sync do the things?
Timber.tag(loggerTag.value).d("PUSH received in a foreground state, ignore")
} else {
coroutineScope.launch(Dispatchers.IO) { onMessageReceivedInternal(pushData) }
coroutineScope.launch(Dispatchers.IO) { handleInternal(pushData) }
}
}
}
@ -115,12 +110,12 @@ class VectorPushHandler @Inject constructor(
*
* @param pushData Object containing message data.
*/
private suspend fun onMessageReceivedInternal(pushData: PushData) {
private suspend fun handleInternal(pushData: PushData) {
try {
if (buildMeta.lowPrivacyLoggingEnabled) {
Timber.tag(loggerTag.value).d("## onMessageReceivedInternal() : $pushData")
Timber.tag(loggerTag.value).d("## handleInternal() : $pushData")
} else {
Timber.tag(loggerTag.value).d("## onMessageReceivedInternal()")
Timber.tag(loggerTag.value).d("## handleInternal()")
}
val session = activeSessionHolder.getOrInitializeSession(startSync = false)
@ -140,7 +135,7 @@ class VectorPushHandler @Inject constructor(
}
}
} catch (e: Exception) {
Timber.tag(loggerTag.value).e(e, "## onMessageReceivedInternal() failed")
Timber.tag(loggerTag.value).e(e, "## handleInternal() failed")
}
}

View file

@ -40,6 +40,7 @@ private val loggerTag = LoggerTag("Push", LoggerTag.SYNC)
@AndroidEntryPoint
class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
@Inject lateinit var pushersManager: PushersManager
@Inject lateinit var pushParser: PushParser
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var vectorPushHandler: VectorPushHandler
@ -57,7 +58,10 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
* @param instance connection, for multi-account
*/
override fun onMessage(context: Context, message: ByteArray, instance: String) {
vectorPushHandler.onMessage(String(message))
Timber.tag(loggerTag.value).d("New message")
pushParser.parsePushDataUnifiedPush(message)?.let {
vectorPushHandler.handle(it)
}
}
override fun onNewEndpoint(context: Context, endpoint: String, instance: String) {