2020-04-15 10:24:30 +10:00
|
|
|
package org.thoughtcrime.securesms.service
|
|
|
|
|
|
|
|
import com.google.firebase.messaging.FirebaseMessagingService
|
|
|
|
import com.google.firebase.messaging.RemoteMessage
|
|
|
|
import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
|
2020-05-12 11:46:11 +10:00
|
|
|
import org.thoughtcrime.securesms.loki.api.LokiPushNotificationManager
|
2020-04-15 10:24:30 +10:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
|
|
|
import org.whispersystems.libsignal.logging.Log
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
|
|
|
|
import org.whispersystems.signalservice.internal.util.Base64
|
2020-05-07 17:59:41 +10:00
|
|
|
import org.whispersystems.signalservice.loki.api.LokiMessageWrapper
|
2020-04-15 10:24:30 +10:00
|
|
|
|
2020-04-16 16:56:12 +10:00
|
|
|
class PushNotificationService : FirebaseMessagingService() {
|
2020-04-15 10:24:30 +10:00
|
|
|
|
|
|
|
override fun onNewToken(token: String) {
|
|
|
|
super.onNewToken(token)
|
2020-04-16 16:56:12 +10:00
|
|
|
Log.d("Loki", "New FCM token: $token.")
|
2020-04-17 12:11:27 +10:00
|
|
|
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this) ?: return
|
2020-04-17 13:26:11 +10:00
|
|
|
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this, false)
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onMessageReceived(message: RemoteMessage) {
|
|
|
|
val base64EncodedData = message.data["ENCRYPTED_DATA"]
|
|
|
|
val data = base64EncodedData?.let { Base64.decode(it) }
|
|
|
|
if (data != null) {
|
|
|
|
try {
|
|
|
|
val envelope = LokiMessageWrapper.unwrap(data)
|
|
|
|
PushContentReceiveJob(this).processEnvelope(SignalServiceEnvelope(envelope))
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.d("Loki", "Failed to unwrap data for message.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Log.d("Loki", "Failed to decode data for message.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|