From 288b70bb1467bab0d957c3cd0970b56eeefdca27 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 13 Jun 2023 10:21:52 +0930 Subject: [PATCH] Store legacy fcm token to reduce unregister api calls --- .../notifications/PushNotificationAPI.kt | 16 ++++++++++++---- .../utilities/TextSecurePreferences.kt | 13 ++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushNotificationAPI.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushNotificationAPI.kt index 32bd17c8c2..9d36ff0013 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushNotificationAPI.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushNotificationAPI.kt @@ -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}.") diff --git a/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt b/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt index 9c13699dda..a1482d96b4 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt @@ -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, "")