Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

/*
Expand Down Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading