add update token guard and move acknowledgement to signal service

This commit is contained in:
ryanzhao 2020-04-15 15:03:57 +10:00
parent 18ce4433d2
commit 8ea2fe0294
3 changed files with 28 additions and 26 deletions

View File

@ -10,7 +10,7 @@ import java.io.IOException
object LokiPushNotificationManager {
//const val server = "https://live.apns.getsession.org/"
const val server = "https://dev.apns.getsession.org/"
const val tokenExpirationInterval = 2 * 24 * 60 * 60
const val tokenExpirationInterval = 2 * 24 * 60 * 60 * 1000
private val connection = OkHttpClient()
fun disableRemoteNotification(token: String, context: Context?) {
@ -43,6 +43,10 @@ object LokiPushNotificationManager {
@JvmStatic
fun register(token: String, hexEncodedPublicKey: String, context: Context?) {
if (token == TextSecurePreferences.getTokenForRemoteNotification(context) && System.currentTimeMillis() - TextSecurePreferences.getLastTimeForTokenUploading(context) < 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))
@ -57,6 +61,8 @@ object LokiPushNotificationManager {
val code = json?.get("code") as? Int
if (code != null && code != 0) {
TextSecurePreferences.setIsUsingRemoteNotification(context, true)
TextSecurePreferences.setTokenForRemoteNotification(context, token)
TextSecurePreferences.setLastTimeForTokenUploading(context, System.currentTimeMillis())
} else {
Log.d("Loki", "Couldn't register device token due to error: ${json?.get("message") as? String}.")
}
@ -70,29 +76,5 @@ object LokiPushNotificationManager {
})
}
fun acknowledgeDeliveryForMessageWith(hash: String, expiration: Int, hexEncodedPublicKey: String, context: Context?) {
val parameters = mapOf("hash" to hash, "pubKey" to hexEncodedPublicKey, "expiration" to expiration)
val url = "${server}acknowledge_message_delivery"
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) {
Log.d("Loki", "Couldn't acknowledge the delivery for message due to error: ${json?.get("message") as? String}.")
}
}
}
}
override fun onFailure(call: Call, exception: IOException) {
Log.d("Loki", "Couldn't acknowledge the delivery for message with last hash: ${hash}")
}
})
}
}

View File

@ -6,6 +6,7 @@ import nl.komponents.kovenant.functional.map
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
import org.thoughtcrime.securesms.loki.LokiPushNotificationManager
import org.thoughtcrime.securesms.service.PersistentAlarmManagerListener
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
@ -15,7 +16,7 @@ import java.util.concurrent.TimeUnit
class BackgroundPollWorker : PersistentAlarmManagerListener() {
companion object {
private val pollInterval = TimeUnit.MINUTES.toMillis(20)
private val pollInterval = TimeUnit.MINUTES.toMillis(2)
@JvmStatic
fun schedule(context: Context) {

View File

@ -185,7 +185,10 @@ public class TextSecurePreferences {
private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode";
//Session for Push Notification
private static final String IS_USING_REMOTE_NOTIFICATION = "pref_is_using_remote_notification";
private static final String TOKEN_FOR_REMOTE_NOTIFICATION = "pref_token_for_remote_notification";
private static final String LAST_TIME_FOR_TOKEN_UPLOADING = "pref_last_time_for_token_uploading";
public static boolean isUsingRemoteNotification(Context context) {
return getBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, false);
@ -195,6 +198,22 @@ public class TextSecurePreferences {
setBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, value);
}
public static String getTokenForRemoteNotification(Context context) {
return getStringPreference(context, TOKEN_FOR_REMOTE_NOTIFICATION, "");
}
public static void setTokenForRemoteNotification(Context context, String value) {
setStringPreference(context, TOKEN_FOR_REMOTE_NOTIFICATION, value);
}
public static long getLastTimeForTokenUploading(Context context) {
return getLongPreference(context, LAST_TIME_FOR_TOKEN_UPLOADING, 0);
}
public static void setLastTimeForTokenUploading(Context context, long value) {
setLongPreference(context, LAST_TIME_FOR_TOKEN_UPLOADING, value);
}
public static boolean isScreenLockEnabled(@NonNull Context context) {
return getBooleanPreference(context, SCREEN_LOCK, false);
}