Store legacy fcm token to reduce unregister api calls

This commit is contained in:
andrew 2023-06-13 10:21:52 +09:30
parent b2a1b5fe46
commit 288b70bb14
2 changed files with 24 additions and 5 deletions

View File

@ -32,7 +32,7 @@ object PushNotificationAPI {
fun register(token: String? = TextSecurePreferences.getFCMToken(context)) {
Log.d(TAG, "register: $token")
token?.let(::unregisterV1)
unregisterV1IfRequired()
subscribeGroups()
}
@ -40,11 +40,16 @@ object PushNotificationAPI {
fun unregister(token: String) {
Log.d(TAG, "unregister: $token")
unregisterV1(token)
unregisterV1IfRequired()
unsubscribeGroups()
}
private fun unregisterV1(token: String) {
/**
* Unregister push notifications for 1-1 conversations as this is now done in FirebasePushManager.
*/
private fun unregisterV1IfRequired() {
val token = TextSecurePreferences.getLegacyFCMToken(context) ?: return
val parameters = mapOf( "token" to token )
val url = "$legacyServer/unregister"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
@ -53,7 +58,10 @@ object PushNotificationAPI {
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
when (response.info["code"]) {
null, 0 -> Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${response.info["message"]}.")
else -> Log.d(TAG, "unregisterV1 success token: $token")
else -> {
TextSecurePreferences.clearLegacyFCMToken(context)
Log.d(TAG, "unregisterV1 success token: $token")
}
}
}.fail { exception ->
Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${exception}.")

View File

@ -250,7 +250,8 @@ interface TextSecurePreferences {
const val GIF_METADATA_WARNING = "has_seen_gif_metadata_warning"
const val GIF_GRID_LAYOUT = "pref_gif_grid_layout"
const val IS_USING_FCM = "pref_is_using_fcm"
const val FCM_TOKEN = "pref_fcm_token"
const val FCM_TOKEN_LEGACY = "pref_fcm_token"
const val FCM_TOKEN = "pref_fcm_token_2"
const val LAST_FCM_TOKEN_UPLOAD_TIME = "pref_last_fcm_token_upload_time_2"
const val LAST_CONFIGURATION_SYNC_TIME = "pref_last_configuration_sync_time"
const val CONFIGURATION_SYNCED = "pref_configuration_synced"
@ -312,6 +313,16 @@ interface TextSecurePreferences {
setBooleanPreference(context, IS_USING_FCM, value)
}
@JvmStatic
fun getLegacyFCMToken(context: Context): String? {
return getStringPreference(context, FCM_TOKEN_LEGACY, "")
}
@JvmStatic
fun clearLegacyFCMToken(context: Context) {
removePreference(context, FCM_TOKEN_LEGACY)
}
@JvmStatic
fun getFCMToken(context: Context): String? {
return getStringPreference(context, FCM_TOKEN, "")