-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Compose Firebase InAppMessaging #1498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
...saging/app/src/main/java/com/google/firebase/fiamquickstart/kotlin/ComposeMainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package com.google.firebase.fiamquickstart.kotlin | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.compose.setContent | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.compose.foundation.Image | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.material.Button | ||
| import androidx.compose.material.ButtonDefaults | ||
| import androidx.compose.material.MaterialTheme | ||
| import androidx.compose.material.Scaffold | ||
| import androidx.compose.material.SnackbarHostState | ||
| import androidx.compose.material.Surface | ||
| import androidx.compose.material.Text | ||
| import androidx.compose.material.rememberScaffoldState | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.State | ||
| import androidx.compose.runtime.collectAsState | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.res.colorResource | ||
| import androidx.compose.ui.res.painterResource | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.compose.ui.unit.sp | ||
| import androidx.lifecycle.viewmodel.compose.viewModel | ||
| import com.google.firebase.fiamquickstart.R | ||
| import com.google.firebase.fiamquickstart.kotlin.ui.theme.InAppMessagingTheme | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| class ComposeMainActivity : AppCompatActivity() { | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| setContent { | ||
| InAppMessagingTheme { | ||
| Surface( | ||
| modifier = Modifier.fillMaxSize(), | ||
| color = MaterialTheme.colors.background | ||
| ) { | ||
| MainAppView() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "Compose-FIAM-Quickstart" | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| fun MainAppView( | ||
| inAppMessagingViewModel: InAppMessagingViewModel = viewModel(factory = InAppMessagingViewModel.Factory) | ||
| ) { | ||
| val snackbarHostState = remember { SnackbarHostState() } | ||
| val scope = rememberCoroutineScope() | ||
| val scaffoldState = rememberScaffoldState(snackbarHostState = snackbarHostState) | ||
|
|
||
| Scaffold( | ||
| scaffoldState = scaffoldState, | ||
| content = { | ||
| Column( | ||
| modifier = Modifier | ||
| .fillMaxSize() | ||
| .padding(it) | ||
| ) { | ||
| MainContent( | ||
| _installationsId = inAppMessagingViewModel.installationsId.collectAsState(""), | ||
| triggerEvent = { | ||
| inAppMessagingViewModel.triggerEvent() | ||
| scope.launch { | ||
| snackbarHostState.showSnackbar("engagement_party' event triggered!") | ||
thatfiredev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| fun MainContent( | ||
| _installationsId: State<String> = mutableStateOf(""), | ||
| triggerEvent: ()-> Unit = {} | ||
| ) { | ||
| val installationsId by _installationsId | ||
|
|
||
| Column( | ||
| modifier = Modifier | ||
| .padding(16.dp) | ||
| .fillMaxWidth(), | ||
| ) { | ||
| Image( | ||
| painter = painterResource(R.drawable.firebase_lockup_400), | ||
| contentDescription = "Firebase logo", | ||
| modifier = Modifier | ||
| .fillMaxWidth() | ||
| .padding(8.dp), | ||
| alignment = Alignment.Center | ||
| ) | ||
|
|
||
| Text( | ||
| text = stringResource(R.string.textview_text), | ||
| fontSize = 18.sp, | ||
| style = MaterialTheme.typography.h6, | ||
| modifier = Modifier.padding(top = 8.dp, start = 8.dp, end = 8.dp) | ||
| ) | ||
|
|
||
| Text( | ||
| text = stringResource(R.string.warning_fresh_install), | ||
| fontSize = 18.sp, | ||
| style = MaterialTheme.typography.h6, | ||
| modifier = Modifier.padding(top = 16.dp, start = 8.dp, end = 8.dp, bottom = 8.dp) | ||
| ) | ||
|
|
||
| Text( | ||
| text = if(installationsId.isEmpty()){ | ||
| "Fetching Installation ID..." | ||
| } else { | ||
| stringResource(R.string.installation_id_fmt, installationsId) | ||
| }, | ||
thatfiredev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fontSize = 18.sp, | ||
| style = MaterialTheme.typography.h6, | ||
| modifier = Modifier.padding(top = 16.dp, start = 8.dp, end = 8.dp, bottom = 8.dp) | ||
| ) | ||
|
|
||
| Button( | ||
| modifier = Modifier | ||
| .fillMaxWidth() | ||
| .padding(8.dp), | ||
| colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(R.color.colorPrimary)), | ||
| onClick = { | ||
| triggerEvent() | ||
| } | ||
| ) { | ||
| Text( | ||
| text = stringResource(R.string.button_text).uppercase(), | ||
| color = Color.White | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @Composable | ||
| @Preview(showBackground = true) | ||
| fun MainContentPreview(){ | ||
| InAppMessagingTheme { | ||
| MainContent() | ||
| } | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
...ng/app/src/main/java/com/google/firebase/fiamquickstart/kotlin/InAppMessagingViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.google.firebase.fiamquickstart.kotlin | ||
|
|
||
| import android.os.Bundle | ||
| import android.util.Log | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import androidx.lifecycle.viewModelScope | ||
| import androidx.lifecycle.viewmodel.CreationExtras | ||
| import com.google.firebase.analytics.FirebaseAnalytics | ||
| import com.google.firebase.analytics.ktx.analytics | ||
| import com.google.firebase.inappmessaging.FirebaseInAppMessaging | ||
| import com.google.firebase.inappmessaging.ktx.inAppMessaging | ||
| import com.google.firebase.installations.FirebaseInstallations | ||
| import com.google.firebase.installations.ktx.installations | ||
| import com.google.firebase.ktx.Firebase | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.tasks.await | ||
|
|
||
| class InAppMessagingViewModel( | ||
| private val firebaseAnalytics: FirebaseAnalytics, | ||
| firebaseIam: FirebaseInAppMessaging, | ||
| private val firebaseInstallations: FirebaseInstallations, | ||
| ): ViewModel() { | ||
|
|
||
| private val _installationsId = MutableStateFlow("") | ||
| val installationsId: StateFlow<String> = _installationsId | ||
|
|
||
| init { | ||
| firebaseIam.isAutomaticDataCollectionEnabled = true | ||
| firebaseIam.setMessagesSuppressed(false) | ||
|
|
||
| viewModelScope.launch { | ||
| try { | ||
| val id = firebaseInstallations.id.await() | ||
| _installationsId.value = id | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Error fetching installation ID", e) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun triggerEvent(){ | ||
| firebaseAnalytics.logEvent("engagement_party", Bundle()) | ||
| } | ||
|
|
||
| companion object { | ||
| const val TAG = "InAppMessagingViewModel" | ||
|
|
||
| // Used to inject this ViewModel's dependencies | ||
| // See also: https://developer.android.com/topic/libraries/architecture/viewmodel/viewmodel-factories | ||
| val Factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory { | ||
| @Suppress("UNCHECKED_CAST") | ||
| override fun <T : ViewModel> create( | ||
| modelClass: Class<T>, | ||
| extras: CreationExtras | ||
| ): T { | ||
| val firebaseAnalytics = Firebase.analytics | ||
| val firebaseIam = Firebase.inAppMessaging | ||
| val firebaseInstallations = Firebase.installations | ||
| return InAppMessagingViewModel(firebaseAnalytics, firebaseIam, firebaseInstallations) as T | ||
| } | ||
| } | ||
| } | ||
| } |
36 changes: 13 additions & 23 deletions
36
...ssaging/app/src/main/java/com/google/firebase/fiamquickstart/kotlin/KotlinMainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,41 @@ | ||
| package com.google.firebase.fiamquickstart.kotlin | ||
|
|
||
| import android.os.Bundle | ||
| import android.util.Log | ||
| import androidx.activity.viewModels | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.google.android.material.snackbar.Snackbar | ||
| import com.google.firebase.analytics.FirebaseAnalytics | ||
| import com.google.firebase.analytics.ktx.analytics | ||
| import com.google.firebase.fiamquickstart.R | ||
| import com.google.firebase.fiamquickstart.databinding.ActivityMainBinding | ||
| import com.google.firebase.inappmessaging.FirebaseInAppMessaging | ||
| import com.google.firebase.inappmessaging.ktx.inAppMessaging | ||
| import com.google.firebase.installations.ktx.installations | ||
| import com.google.firebase.ktx.Firebase | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| class KotlinMainActivity : AppCompatActivity() { | ||
|
|
||
| private lateinit var firebaseAnalytics: FirebaseAnalytics | ||
| private lateinit var firebaseIam: FirebaseInAppMessaging | ||
| private val viewModel: InAppMessagingViewModel by viewModels { InAppMessagingViewModel.Factory } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| val binding = ActivityMainBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
|
|
||
| firebaseAnalytics = Firebase.analytics | ||
| firebaseIam = Firebase.inAppMessaging | ||
|
|
||
| firebaseIam.isAutomaticDataCollectionEnabled = true | ||
| firebaseIam.setMessagesSuppressed(false) | ||
|
|
||
| binding.eventTriggerButton.setOnClickListener { view -> | ||
| firebaseAnalytics.logEvent("engagement_party", Bundle()) | ||
| viewModel.triggerEvent() | ||
| Snackbar.make(view, "'engagement_party' event triggered!", Snackbar.LENGTH_LONG) | ||
| .setAction("Action", null) | ||
| .show() | ||
| .setAction("Action", null) | ||
| .show() | ||
| } | ||
|
|
||
| // Get and display/log the installation id | ||
| Firebase.installations.getId() | ||
| .addOnSuccessListener { id -> | ||
| binding.installationIdText.text = getString(R.string.installation_id_fmt, id) | ||
| Log.d(TAG, "Installation ID: $id") | ||
| lifecycleScope.launch { | ||
| viewModel.installationsId.collect { | ||
| if (it.isNotEmpty()) { | ||
| binding.installationIdText.text = getString(R.string.installation_id_fmt, it) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
|
|
||
| private const val TAG = "FIAM-Quickstart" | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
inappmessaging/app/src/main/java/com/google/firebase/fiamquickstart/kotlin/theme/Color.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.google.firebase.fiamquickstart.kotlin.ui.theme | ||
|
|
||
| import androidx.compose.ui.graphics.Color | ||
|
|
||
| val Purple80 = Color(0xFFD0BCFF) | ||
| val PurpleGrey80 = Color(0xFFCCC2DC) | ||
| val Pink80 = Color(0xFFEFB8C8) | ||
|
|
||
| val FirebaseBlue = Color(0xFF0288D1) // copied from colors.xml | ||
| val FirebaseBannerBlue = Color(0xFF039BE5) // copied from colors.xml | ||
| val FirebaseOrange = Color(0xFFFFA000) // copied from colors.xml |
11 changes: 11 additions & 0 deletions
11
inappmessaging/app/src/main/java/com/google/firebase/fiamquickstart/kotlin/theme/Shape.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.google.firebase.fiamquickstart.kotlin.ui.theme | ||
|
|
||
| import androidx.compose.foundation.shape.RoundedCornerShape | ||
| import androidx.compose.material.Shapes | ||
| import androidx.compose.ui.unit.dp | ||
|
|
||
| val Shapes = Shapes( | ||
| small = RoundedCornerShape(16.dp), | ||
| medium = RoundedCornerShape(2.dp), | ||
| large = RoundedCornerShape(0.dp) | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
TAGconstant is declared but never used withinComposeMainActivity. It can be removed to clean up the code.