feat: Enhance connection handling and add debug logs feature; improve user experience and troubleshooting

This commit is contained in:
k1ngsterr1
2026-01-17 06:21:26 +05:00
parent a64ee04b55
commit a9e426506b
9 changed files with 148 additions and 145 deletions

View File

@@ -91,10 +91,6 @@ class MainActivity : ComponentActivity() {
val notificationPermissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(),
onResult = { isGranted ->
Log.d(TAG, "📬 Notification permission result: $isGranted")
if (!isGranted) {
Log.w(TAG, "⚠️ User denied notification permission")
}
}
)
@@ -107,10 +103,7 @@ class MainActivity : ComponentActivity() {
) == PackageManager.PERMISSION_GRANTED
if (!hasPermission) {
Log.d(TAG, "📬 Requesting notification permission...")
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
} else {
Log.d(TAG, "✅ Notification permission already granted")
}
}
}
@@ -289,13 +282,10 @@ class MainActivity : ComponentActivity() {
// Получаем FCM токен
FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
if (!task.isSuccessful) {
Log.e(TAG, "❌ Failed to get FCM token", task.exception)
return@addOnCompleteListener
}
val token = task.result
Log.d(TAG, "🔔 FCM token (short): ${token?.take(20)}...")
Log.d(TAG, "🔔 FCM token (FULL): $token")
// Сохраняем токен локально
token?.let { saveFcmToken(it) }
@@ -304,9 +294,7 @@ class MainActivity : ComponentActivity() {
// (см. вызов sendFcmTokenToServer в onAccountLogin)
}
Log.d(TAG, "✅ Firebase initialized successfully")
} catch (e: Exception) {
Log.e(TAG, "❌ Error initializing Firebase", e)
}
}
@@ -316,7 +304,6 @@ class MainActivity : ComponentActivity() {
private fun saveFcmToken(token: String) {
val prefs = getSharedPreferences("rosetta_prefs", MODE_PRIVATE)
prefs.edit().putString("fcm_token", token).apply()
Log.d(TAG, "💾 FCM token saved locally")
}
/**
@@ -330,38 +317,27 @@ class MainActivity : ComponentActivity() {
val token = prefs.getString("fcm_token", null)
if (token == null) {
Log.d(TAG, "⚠️ Cannot send FCM token: Token not found")
return@launch
}
// 🔥 КРИТИЧНО: Ждем пока протокол станет 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++
}
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 (packet ID: 0x10)")
} catch (e: Exception) {
Log.e(TAG, "❌ Error sending FCM token to server", e)
}
}
}