feat: Integrate Firebase Cloud Messaging for push notifications; add service to handle token and message reception
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.rosetta.messenger
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
@@ -27,10 +28,14 @@ import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.google.firebase.FirebaseApp
|
||||
import com.google.firebase.messaging.FirebaseMessaging
|
||||
import com.rosetta.messenger.crypto.CryptoManager
|
||||
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.ProtocolManager
|
||||
import com.rosetta.messenger.ui.auth.AccountInfo
|
||||
import com.rosetta.messenger.ui.auth.AuthFlow
|
||||
@@ -49,6 +54,10 @@ class MainActivity : ComponentActivity() {
|
||||
private lateinit var preferencesManager: PreferencesManager
|
||||
private lateinit var accountManager: AccountManager
|
||||
|
||||
companion object {
|
||||
private const val TAG = "MainActivity"
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
@@ -60,6 +69,9 @@ class MainActivity : ComponentActivity() {
|
||||
// 🔥 Инициализируем ProtocolManager для обработки онлайн статусов
|
||||
ProtocolManager.initialize(this)
|
||||
|
||||
// 🔔 Инициализируем Firebase для push-уведомлений
|
||||
initializeFirebase()
|
||||
|
||||
// 🚀 Предзагружаем эмодзи в фоне для мгновенного открытия пикера
|
||||
// Используем новый оптимизированный кэш
|
||||
OptimizedEmojiCache.preload(this)
|
||||
@@ -153,7 +165,7 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
)
|
||||
}
|
||||
"auth_new", "auth_new", "auth_unlock" -> {
|
||||
"auth_new", "auth_unlock" -> {
|
||||
AuthFlow(
|
||||
isDarkTheme = isDarkTheme,
|
||||
hasExistingAccount = screen == "auth_unlock",
|
||||
@@ -223,6 +235,47 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 🔔 Инициализация Firebase Cloud Messaging
|
||||
*/
|
||||
private fun initializeFirebase() {
|
||||
try {
|
||||
// Инициализируем Firebase
|
||||
FirebaseApp.initializeApp(this)
|
||||
|
||||
// Получаем 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) }
|
||||
|
||||
// TODO: Отправляем токен на сервер если аккаунт залогинен
|
||||
// token?.let { sendFcmTokenToServer(it) }
|
||||
}
|
||||
|
||||
Log.d(TAG, "✅ Firebase initialized successfully")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "❌ Error initializing Firebase", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Сохранить FCM токен в SharedPreferences
|
||||
*/
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@@ -333,12 +386,12 @@ fun MainScreen(
|
||||
}
|
||||
},
|
||||
label = "screenNavigation"
|
||||
) { (user, isSearchOpen, _) ->
|
||||
) { (currentUser, isSearchOpen, _) ->
|
||||
when {
|
||||
user != null -> {
|
||||
currentUser != null -> {
|
||||
// Экран чата
|
||||
ChatDetailScreen(
|
||||
user = user,
|
||||
user = currentUser,
|
||||
currentUserPublicKey = accountPublicKey,
|
||||
currentUserPrivateKey = accountPrivateKey,
|
||||
isDarkTheme = isDarkTheme,
|
||||
@@ -365,9 +418,9 @@ fun MainScreen(
|
||||
isDarkTheme = isDarkTheme,
|
||||
protocolState = protocolState,
|
||||
onBackClick = { showSearchScreen = false },
|
||||
onUserSelect = { user ->
|
||||
onUserSelect = { selectedSearchUser ->
|
||||
showSearchScreen = false
|
||||
selectedUser = user
|
||||
selectedUser = selectedSearchUser
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -394,7 +447,14 @@ fun MainScreen(
|
||||
// TODO: Navigate to calls
|
||||
},
|
||||
onSavedMessagesClick = {
|
||||
// TODO: Navigate to saved messages
|
||||
// Открываем чат с самим собой (Saved Messages)
|
||||
selectedUser = SearchUser(
|
||||
title = "Saved Messages",
|
||||
username = "",
|
||||
publicKey = accountPublicKey,
|
||||
verified = 0,
|
||||
online = 1
|
||||
)
|
||||
},
|
||||
onSettingsClick = {
|
||||
// TODO: Navigate to settings
|
||||
@@ -408,8 +468,8 @@ fun MainScreen(
|
||||
onNewChat = {
|
||||
// TODO: Show new chat screen
|
||||
},
|
||||
onUserSelect = { user ->
|
||||
selectedUser = user
|
||||
onUserSelect = { selectedChatUser ->
|
||||
selectedUser = selectedChatUser
|
||||
},
|
||||
onLogout = onLogout
|
||||
)
|
||||
@@ -417,3 +477,4 @@ fun MainScreen(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user