feat: Implement FCM token handling and dialog cache management; enhance user experience and performance

This commit is contained in:
k1ngsterr1
2026-01-17 05:53:27 +05:00
parent c9724b3bb7
commit 52ffc22763
9 changed files with 333 additions and 65 deletions

View File

@@ -43,8 +43,10 @@ import com.rosetta.messenger.data.AccountManager
import com.rosetta.messenger.data.DecryptedAccount
import com.rosetta.messenger.data.PreferencesManager
import com.rosetta.messenger.data.RecentSearchesManager
import com.rosetta.messenger.network.PacketPushToken
import com.rosetta.messenger.network.PacketPushNotification
import com.rosetta.messenger.network.PushNotificationAction
import com.rosetta.messenger.network.ProtocolManager
import com.rosetta.messenger.network.ProtocolState
import com.rosetta.messenger.ui.auth.AccountInfo
import com.rosetta.messenger.ui.auth.AuthFlow
import com.rosetta.messenger.ui.chats.ChatsListScreen
@@ -57,6 +59,7 @@ import com.rosetta.messenger.ui.onboarding.OnboardingScreen
import com.rosetta.messenger.ui.splash.SplashScreen
import com.rosetta.messenger.ui.theme.RosettaAndroidTheme
import kotlinx.coroutines.launch
import kotlinx.coroutines.delay
class MainActivity : ComponentActivity() {
private lateinit var preferencesManager: PreferencesManager
@@ -331,19 +334,32 @@ class MainActivity : ComponentActivity() {
return@launch
}
Log.d(TAG, "📤 Sending FCM token to server...")
Log.d(TAG, " Token (short): ${token.take(20)}...")
Log.d(TAG, " PublicKey: ${account.publicKey.take(20)}...")
// 🔥 КРИТИЧНО: Ждем пока протокол станет AUTHENTICATED
var waitAttempts = 0
while (ProtocolManager.state.value != ProtocolState.AUTHENTICATED && waitAttempts < 50) {
Log.d(TAG, "⏳ Waiting for protocol to be authenticated... (attempt ${waitAttempts + 1}/50)")
delay(100) // Ждем 100ms
waitAttempts++
}
val packet = PacketPushToken().apply {
this.privateKey = account.privateKey
this.publicKey = account.publicKey
this.pushToken = token
this.platform = "android"
if (ProtocolManager.state.value != ProtocolState.AUTHENTICATED) {
Log.e(TAG, "❌ Cannot send FCM token: Protocol not authenticated after 5 seconds")
return@launch
}
Log.d(TAG, "📤 Sending FCM token to server (new format 0x10)...")
Log.d(TAG, " Token (short): ${token.take(20)}...")
Log.d(TAG, " Token (FULL): $token")
Log.d(TAG, " Action: SUBSCRIBE")
Log.d(TAG, " Protocol state: ${ProtocolManager.state.value}")
val packet = PacketPushNotification().apply {
this.notificationsToken = token
this.action = PushNotificationAction.SUBSCRIBE
}
ProtocolManager.send(packet)
Log.d(TAG, "✅ FCM token sent to server")
Log.d(TAG, "✅ FCM token sent to server (packet ID: 0x10)")
} catch (e: Exception) {
Log.e(TAG, "❌ Error sending FCM token to server", e)
}