From 334cf4776f314157018f1f2b32593ca986a7c211 Mon Sep 17 00:00:00 2001 From: Wesley William Supangat <44500655+supwess@users.noreply.github.com> Date: Sat, 28 Nov 2026 05:49:26 -0600 Subject: [PATCH 1/2] fixed token register --- .../model/services/FcmTokenRegistrar.kt | 45 ++++++++----------- .../services/FirebaseNotificationService.kt | 7 ++- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/code/app/src/main/java/com/example/lambda_bbb_events/model/services/FcmTokenRegistrar.kt b/code/app/src/main/java/com/example/lambda_bbb_events/model/services/FcmTokenRegistrar.kt index 0955ccf7..024087f9 100644 --- a/code/app/src/main/java/com/example/lambda_bbb_events/model/services/FcmTokenRegistrar.kt +++ b/code/app/src/main/java/com/example/lambda_bbb_events/model/services/FcmTokenRegistrar.kt @@ -3,44 +3,37 @@ package com.example.lambda_bbb_events.model.services import android.util.Log import com.google.firebase.messaging.FirebaseMessaging import com.example.lambda_bbb_events.BuildConfig +import com.google.firebase.Firebase import com.google.firebase.FirebaseApp +import com.google.firebase.firestore.FieldValue +import com.google.firebase.firestore.firestore /** * Retrieves the Firebase Cloud Messaging registration token * for the current device. */ object FcmTokenRegistrar { - /** - * Retrieves the FCM token for the specified device. + * Fetches the current token and saves it for [deviceId]. * - * @param deviceId Unique identifier of the device. + * @param deviceId The ID of the current device. */ fun register(deviceId: String) { - - val options = FirebaseApp.getInstance().options - - Log.d("FCM", "Project ID: ${options.projectId}") - Log.d("FCM", "Sender ID: ${options.gcmSenderId}") - Log.d("FCM", "Application ID: ${options.applicationId}") - FirebaseMessaging.getInstance().token - .addOnSuccessListener { token -> - - Log.d("FCM", "Device: $deviceId") - Log.d("FCM", "Token retrieved successfully") - - if (BuildConfig.DEBUG) { - Log.d("FCM", "Token: $token") - } - } - .addOnFailureListener { exception -> + .addOnSuccessListener { token -> save(deviceId, token) } + .addOnFailureListener { Log.e("FCM", "Failed to get FCM token", it) } + } - Log.e( - "FCM", - "Failed to retrieve FCM token", - exception - ) - } + /** + * Writes the token to fcmTokens/{deviceId}. Also called when the token refreshes. + * + * @param deviceId The ID of the current device. + * @param token The FCM token. + */ + fun save(deviceId: String, token: String) { + Firebase.firestore.collection("fcmTokens").document(deviceId) + .set(mapOf("token" to token, "updatedAt" to FieldValue.serverTimestamp())) + .addOnSuccessListener { Log.d("FCM", "Token saved for $deviceId") } + .addOnFailureListener { Log.e("FCM", "Failed to save token", it) } } } \ No newline at end of file diff --git a/code/app/src/main/java/com/example/lambda_bbb_events/model/services/FirebaseNotificationService.kt b/code/app/src/main/java/com/example/lambda_bbb_events/model/services/FirebaseNotificationService.kt index 731940cb..2e4b7438 100644 --- a/code/app/src/main/java/com/example/lambda_bbb_events/model/services/FirebaseNotificationService.kt +++ b/code/app/src/main/java/com/example/lambda_bbb_events/model/services/FirebaseNotificationService.kt @@ -47,7 +47,10 @@ class FirebaseNotificationService : FirebaseMessagingService() { override fun onNewToken(token: String) { super.onNewToken(token) - - Log.d("FCM", "Token refreshed") + val deviceId = android.provider.Settings.Secure.getString( + applicationContext.contentResolver, + android.provider.Settings.Secure.ANDROID_ID + ) + FcmTokenRegistrar.save(deviceId, token) } } \ No newline at end of file From b5e5c37688a30db76ed072f29bbbe2464ffbb01b Mon Sep 17 00:00:00 2001 From: Wesley William Supangat <44500655+supwess@users.noreply.github.com> Date: Sat, 28 Nov 2026 06:31:15 -0600 Subject: [PATCH 2/2] deleted local notif listener since we dont need it anymore --- .../com/example/lambda_bbb_events/MainActivity.kt | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/code/app/src/main/java/com/example/lambda_bbb_events/MainActivity.kt b/code/app/src/main/java/com/example/lambda_bbb_events/MainActivity.kt index 31a8afd6..72e93968 100644 --- a/code/app/src/main/java/com/example/lambda_bbb_events/MainActivity.kt +++ b/code/app/src/main/java/com/example/lambda_bbb_events/MainActivity.kt @@ -55,7 +55,6 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.repeatOnLifecycle import com.example.lambda_bbb_events.model.services.FirebaseNotificationService import kotlinx.coroutines.flow.collect -import com.example.lambda_bbb_events.model.services.SystemNotificationListener import kotlinx.coroutines.delay /* @@ -162,23 +161,12 @@ class MainActivity : ComponentActivity() { notificationController = controllers.messageNotifications ) - val systemNotificationListener = SystemNotificationListener( - notificationController = controllers.notifications - ) - setContent { val currentUser = controllers.users.currentUser val currentUserId = currentUser?.deviceId val isAdmin = currentUser?.role == UserRole.ADMIN - // Listen for lottery and reservation notifications. - LaunchedEffect(currentUserId) { - if (currentUserId != null) { - systemNotificationListener.listen(currentUserId) - } - } - // Check for pending weekly notification summaries. LaunchedEffect(currentUserId) {