From be4d742e8467f01e14833ad4481c5f51642f6a4d Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 9 Jun 2023 18:10:06 +0930 Subject: [PATCH] Unregister v1 push --- .../notifications/FirebasePushManager.kt | 26 ++++++++++++------- .../notifications/PushNotificationAPI.kt | 26 ++++++++++++------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt index 702d686122..8015030795 100644 --- a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt +++ b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt @@ -41,6 +41,8 @@ import org.session.libsignal.utilities.retryIfNeeded import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.thoughtcrime.securesms.crypto.KeyPairUtilities +private const val TAG = "FirebasePushManager" + class FirebasePushManager(private val context: Context, private val prefs: TextSecurePreferences): PushManager { companion object { @@ -87,8 +89,7 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS else check(metadata.data_len == content.size) { "wrong message data size" } - Log.d("Loki", - "Received push for ${metadata.account}/${metadata.namespace}, msg ${metadata.msg_hash}, ${metadata.data_len}B") + Log.d(TAG, "Received push for ${metadata.account}/${metadata.namespace}, msg ${metadata.msg_hash}, ${metadata.data_len}B") return content } @@ -102,11 +103,11 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS } private fun refresh(task: Task, force: Boolean) { + Log.d(TAG, "refresh") + // context in here is Dispatchers.IO if (!task.isSuccessful) { - Log.w( - "Loki", - "FirebaseInstanceId.getInstance().getInstanceId() failed." + task.exception + Log.w(TAG, "FirebaseInstanceId.getInstance().getInstanceId() failed." + task.exception ) return } @@ -143,18 +144,21 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS retryIfNeeded(maxRetryCount) { getResponseBody(request.build()).map { response -> if (response.success == true) { + Log.d(TAG, "Unsubscribe FCM success") TextSecurePreferences.setFCMToken(context, null) - Log.d("Loki", "Unsubscribe FCM success") + PushNotificationAPI.unregister(token) } else { - Log.e("Loki", "Couldn't unregister for FCM due to error: ${response.message}") + Log.e(TAG, "Couldn't unregister for FCM due to error: ${response.message}") } }.fail { exception -> - Log.e("Loki", "Couldn't unregister for FCM due to error: ${exception}.", exception) + Log.e(TAG, "Couldn't unregister for FCM due to error: ${exception}.", exception) } } } private fun register(token: String, publicKey: String, userEd25519Key: KeyPair, force: Boolean, namespaces: List = listOf(Namespace.DEFAULT)) { + Log.d(TAG, "register token: $token") + val oldToken = TextSecurePreferences.getFCMToken(context) val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context) if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) return @@ -184,14 +188,16 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS retryIfNeeded(maxRetryCount) { getResponseBody(request.build()).map { response -> if (response.isSuccess()) { + Log.d(TAG, "Success $token") TextSecurePreferences.setFCMToken(context, token) TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis()) + PushNotificationAPI.register(token) } else { val (_, message) = response.errorInfo() - Log.e("Loki", "Couldn't register for FCM due to error: $message.") + Log.e(TAG, "Couldn't register for FCM due to error: $message.") } }.fail { exception -> - Log.e("Loki", "Couldn't register for FCM due to error: ${exception}.", exception) + Log.e(TAG, "Couldn't register for FCM due to error: ${exception}.", exception) } } } 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 aff4987f8a..d9e4293cf7 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 @@ -15,6 +15,8 @@ import org.session.libsignal.utilities.retryIfNeeded @SuppressLint("StaticFieldLeak") object PushNotificationAPI { + private const val TAG = "PushNotificationAPI" + val context = MessagingModuleConfiguration.shared.context const val server = "https://push.getsession.org" const val serverPublicKey: String = "d7557fe563e2610de876c0ac7341b62f3c82d5eea4b62c702392ea4368f51b3b" @@ -28,28 +30,33 @@ object PushNotificationAPI { } fun register(token: String? = TextSecurePreferences.getFCMToken(context)) { + Log.d(TAG, "register: $token") + token?.let(::unregisterV1) subscribeGroups() } @JvmStatic fun unregister(token: String) { + Log.d(TAG, "unregister: $token") + unregisterV1(token) unsubscribeGroups() } private fun unregisterV1(token: String) { val parameters = mapOf( "token" to token ) - val url = "$server/unregister" + val url = "$legacyServer/unregister" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) - val request = Request.Builder().url(url).post(body) + val request = Request.Builder().url(url).post(body).build() retryIfNeeded(maxRetryCount) { - OnionRequestAPI.sendOnionRequest(request.build(), server, serverPublicKey, Version.V2).map { response -> + OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response -> when (response.info["code"]) { - null, 0 -> Log.d("Loki", "Couldn't disable FCM due to error: ${response.info["message"]}.") + null, 0 -> Log.d(TAG, "Couldn't disable FCM due to error: ${response.info["message"]}.") + else -> Log.d(TAG, "unregisterV1 success token: $token") } }.fail { exception -> - Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.") + Log.d(TAG, "Couldn't disable FCM due to error: ${exception}.") } } } @@ -86,15 +93,16 @@ object PushNotificationAPI { val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey ) val url = "$legacyServer/${operation.rawValue}" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) - val request = Request.Builder().url(url).post(body) + val request = Request.Builder().url(url).post(body).build() retryIfNeeded(maxRetryCount) { - OnionRequestAPI.sendOnionRequest(request.build(), legacyServer, legacyServerPublicKey, Version.V2).map { response -> + OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response -> when (response.info["code"]) { - null, 0 -> Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${response.info["message"]}.") + null, 0 -> Log.d(TAG, "performGroupOperation fail: ${operation.rawValue}: $closedGroupPublicKey due to error: ${response.info["message"]}.") + else -> Log.d(TAG, "performGroupOperation success: ${operation.rawValue}") } }.fail { exception -> - Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${exception}.") + Log.d(TAG, "Couldn't ${operation.rawValue}: $closedGroupPublicKey due to error: ${exception}.") } } }