Thursday, 22 October 2026

Chatting in

users/{uid} phone, displayName, photoUrl, country, birthYear, interests[], lastSeen, isOnline, blockedUids[] chats/{chatId} participants: [uidA, uidB] lastMessage, lastMessageAt, unreadCounts chats/{chatId}/messages/{messageId} senderUid, text, mediaUrl, type, createdAt, deliveredTo[], readBy[] reports/{reportId} reporterUid, reportedUid, messageId, reason, createdA dependencies { implementation(platform("com.google.firebase:firebase-bom:33.7.0")) implementation("com.google.firebase:firebase-auth-ktx") implementation("com.google.firebase:firebase-firestore-ktx") implementation("com.google.firebase:firebase-storage-ktx") implementation("com.google.firebase:firebase-messaging-ktx") implementation("androidx.compose.ui:ui") implementation("androidx.compose.material3:material3") implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.7") }@Composable fun PhoneLoginScreen( onOtpSent: (verificationId: String) -> Unit ) { var phone by remember { mutableStateOf("") } val context = LocalContext.current val auth = FirebaseAuth.getInstance() Column( modifier = Modifier .fillMaxSize() .padding(24.dp), verticalArrangement = Arrangement.Center ) { Text("Enter your phone number", style = MaterialTheme.typography.headlineSmall) OutlinedTextField( value = phone, onValueChange = { phone = it }, label = { Text("+91 98765 43210") }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone), modifier = Modifier.fillMaxWidth() ) Button( onClick = { val options = PhoneAuthOptions.newBuilder(auth) .setPhoneNumber(phone) .setTimeout(60L, TimeUnit.SECONDS) .setActivity(context as Activity) .setCallbacks(object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() { override fun onVerificationCompleted(credential: PhoneAuthCredential) { auth.signInWithCredential(credential) } override fun onVerificationFailed(e: FirebaseException) { Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() } override fun onCodeSent( verificationId: String, token: PhoneAuthProvider.ForceResendingToken ) { onOtpSent(verificationId) } }) .build() PhoneAuthProvider.verifyPhoneNumber(options) }, modifier = Modifier .fillMaxWidth() .padding(top = 24.dp) ) { Text("Send OTP") } } suspend fun sendMessage( chatId: String, senderUid: String, text: String ) { val db = Firebase.firestore val message = hashMapOf( "senderUid" to senderUid, "text" to text.trim(), "type" to "text", "createdAt" to FieldValue.serverTimestamp() ) db.collection("chats") .document(chatId) .collection("messages") .add(message) db.collection("chats").document(chatId) .update( mapOf( "lastMessage" to text.trim(), "lastMessageAt" to FieldValue.serverTimestamp() ) ) }suspend fun sendMessage( chatId: String, senderUid: String, text: String ) { val db = Firebase.firestore val message = hashMapOf( "senderUid" to senderUid, "text" to text.trim(), "type" to "text", "createdAt" to FieldValue.serverTimestamp() ) db.collection("chats") .document(chatId) .collection("messages") .add(message) db.collection("chats").document(chatId) .update( mapOf( "lastMessage" to text.trim(), "lastMessageAt" to FieldValue.serverTimestamp() ) ) }suspend fun sendMessage( chatId: String, senderUid: String, text: String ) { val db = Firebase.firestore val message = hashMapOf( "senderUid" to senderUid, "text" to text.trim(), "type" to "text", "createdAt" to FieldValue.serverTimestamp() ) db.collection("chats") .document(chatId) .collection("messages") .add(message) db.collection("chats").document(chatId) .update( mapOf( "lastMessage" to text.trim(), "lastMessageAt" to FieldValue.serverTimestamp() ) ) } }