Merge pull request #174 from loki-project/push-notifications
Improved Push Notifications Stage 2pull/175/head
commit
e68c2d1cfe
@ -0,0 +1,81 @@
|
||||
package org.thoughtcrime.securesms.loki
|
||||
|
||||
import android.content.Context
|
||||
import okhttp3.*
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
||||
import org.whispersystems.libsignal.logging.Log
|
||||
import org.whispersystems.signalservice.internal.util.JsonUtil
|
||||
import org.whispersystems.signalservice.loki.api.LokiPushNotificationAcknowledgement
|
||||
import java.io.IOException
|
||||
|
||||
object LokiPushNotificationManager {
|
||||
private val connection = OkHttpClient()
|
||||
|
||||
private val server by lazy {
|
||||
LokiPushNotificationAcknowledgement.shared.server
|
||||
}
|
||||
|
||||
private const val tokenExpirationInterval = 2 * 24 * 60 * 60 * 1000
|
||||
|
||||
fun disableFCM(token: String, context: Context?) {
|
||||
val parameters = mapOf( "token" to token )
|
||||
val url = "${server}/register"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
connection.newCall(request).enqueue(object : Callback {
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
when (response.code()) {
|
||||
200 -> {
|
||||
val bodyAsString = response.body()!!.string()
|
||||
val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
|
||||
val code = json?.get("code") as? Int
|
||||
if (code != null && code != 0) {
|
||||
TextSecurePreferences.setIsUsingFCM(context, false)
|
||||
} else {
|
||||
Log.d("Loki", "Couldn't disable FCM due to error: ${json?.get("message") as? String ?: "null"}.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call, exception: IOException) {
|
||||
Log.d("Loki", "Couldn't disable FCM.")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun register(token: String, hexEncodedPublicKey: String, context: Context?) {
|
||||
val oldToken = TextSecurePreferences.getFCMToken(context)
|
||||
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
||||
if (token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
|
||||
val parameters = mapOf( "token" to token, "pubKey" to hexEncodedPublicKey )
|
||||
val url = "${server}/register"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
connection.newCall(request).enqueue(object : Callback {
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
when (response.code()) {
|
||||
200 -> {
|
||||
val bodyAsString = response.body()!!.string()
|
||||
val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
|
||||
val code = json?.get("code") as? Int
|
||||
if (code != null && code != 0) {
|
||||
TextSecurePreferences.setIsUsingFCM(context, true)
|
||||
TextSecurePreferences.setFCMToken(context, token)
|
||||
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
|
||||
} else {
|
||||
Log.d("Loki", "Couldn't register for FCM due to error: ${json?.get("message") as? String ?: "null"}.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call, exception: IOException) {
|
||||
Log.d("Loki", "Couldn't register for FCM.")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package org.thoughtcrime.securesms.service
|
||||
|
||||
import com.google.firebase.messaging.FirebaseMessagingService
|
||||
import com.google.firebase.messaging.RemoteMessage
|
||||
import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
|
||||
import org.thoughtcrime.securesms.loki.LokiPushNotificationManager
|
||||
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
|
||||
import org.whispersystems.signalservice.loki.messaging.LokiMessageWrapper
|
||||
|
||||
class PushNotificationService : FirebaseMessagingService() {
|
||||
|
||||
override fun onNewToken(token: String) {
|
||||
super.onNewToken(token)
|
||||
Log.d("Loki", "New FCM token: $token.")
|
||||
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this)
|
||||
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this)
|
||||
}
|
||||
|
||||
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.")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue