From c3cde89f3926907631ee24ed72547423a688f003 Mon Sep 17 00:00:00 2001 From: lehcar09 Date: Mon, 10 Jun 2024 23:08:34 +0800 Subject: [PATCH 1/5] - Initial Commit --- messaging/app/src/main/AndroidManifest.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/messaging/app/src/main/AndroidManifest.xml b/messaging/app/src/main/AndroidManifest.xml index 7ccf60a01..f517489f2 100644 --- a/messaging/app/src/main/AndroidManifest.xml +++ b/messaging/app/src/main/AndroidManifest.xml @@ -55,6 +55,7 @@ + From ba6914a3112d8e8b4c714219cb9fd366df8d9b55 Mon Sep 17 00:00:00 2001 From: lehcar09 Date: Mon, 10 Jun 2024 23:12:27 +0800 Subject: [PATCH 2/5] - Adding compose for FCM --- messaging/app/build.gradle | 7 +- messaging/app/src/main/AndroidManifest.xml | 2 +- .../quickstart/fcm/EntryChoiceActivity.kt | 20 +- .../fcm/kotlin/ComposeMainActivity.kt | 267 ++++++++++++++++++ .../fcm/kotlin/FirebaseMessagingViewModel.kt | 100 +++++++ .../quickstart/fcm/kotlin/MainActivity.kt | 80 +++--- .../fcm/kotlin/MyFirebaseMessagingService.kt | 2 + .../fcm/kotlin/data/SubscriptionState.kt | 5 + .../quickstart/fcm/kotlin/ui/theme/Color.kt | 11 + .../quickstart/fcm/kotlin/ui/theme/Shape.kt | 11 + .../quickstart/fcm/kotlin/ui/theme/Theme.kt | 38 +++ .../quickstart/fcm/kotlin/ui/theme/Type.kt | 18 ++ messaging/gradle.properties | 2 +- 13 files changed, 509 insertions(+), 54 deletions(-) create mode 100644 messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt create mode 100644 messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/FirebaseMessagingViewModel.kt create mode 100644 messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/data/SubscriptionState.kt create mode 100644 messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Color.kt create mode 100644 messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Shape.kt create mode 100644 messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Theme.kt create mode 100644 messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Type.kt diff --git a/messaging/app/build.gradle b/messaging/app/build.gradle index 9324342ec..777037c86 100644 --- a/messaging/app/build.gradle +++ b/messaging/app/build.gradle @@ -8,12 +8,12 @@ check.dependsOn 'assembleDebugAndroidTest' android { namespace 'com.google.firebase.quickstart.fcm' - compileSdk 33 + compileSdk 34 defaultConfig { applicationId "com.google.firebase.quickstart.fcm" minSdk 21 // minSdk would be 19 without compose - targetSdk 33 + targetSdk 34 versionCode 1 versionName "1.0" multiDexEnabled true @@ -97,6 +97,9 @@ dependencies { implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" implementation 'androidx.activity:activity-compose:1.5.1' + implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0' + implementation("com.google.accompanist:accompanist-permissions:0.30.1") + // Testing dependencies androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' androidTestImplementation 'androidx.test:runner:1.5.1' diff --git a/messaging/app/src/main/AndroidManifest.xml b/messaging/app/src/main/AndroidManifest.xml index f517489f2..0b8f6a73c 100644 --- a/messaging/app/src/main/AndroidManifest.xml +++ b/messaging/app/src/main/AndroidManifest.xml @@ -36,6 +36,7 @@ + - diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/EntryChoiceActivity.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/EntryChoiceActivity.kt index 443a8cac0..2d2f9fc34 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/EntryChoiceActivity.kt +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/EntryChoiceActivity.kt @@ -9,14 +9,18 @@ class EntryChoiceActivity : BaseEntryChoiceActivity() { override fun getChoices(): List { return kotlin.collections.listOf( - Choice( - "Java", - "Run the Firebase Cloud Messaging quickstart written in Java.", - Intent(this, MainActivity::class.java)), - Choice( - "Kotlin", - "Run the Firebase Cloud Messaging written in Kotlin.", - Intent(this, com.google.firebase.quickstart.fcm.kotlin.MainActivity::class.java)) + Choice( + "Java", + "Run the Firebase Cloud Messaging quickstart written in Java.", + Intent(this, MainActivity::class.java)), + Choice( + "Kotlin", + "Run the Firebase Cloud Messaging written in Kotlin.", + Intent(this, com.google.firebase.quickstart.fcm.kotlin.MainActivity::class.java)), + Choice( + "Compose", + "Run the Firebase Cloud Messaging written in Compose.", + Intent(this, com.google.firebase.quickstart.fcm.kotlin.ComposeMainActivity::class.java)) ) } } \ No newline at end of file diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt new file mode 100644 index 000000000..33aba6589 --- /dev/null +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt @@ -0,0 +1,267 @@ +package com.google.firebase.quickstart.fcm.kotlin + +import android.Manifest +import android.app.Activity +import android.content.Context +import android.content.ContextWrapper +import android.os.Build +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.compose.setContent +import androidx.activity.result.contract.ActivityResultContracts +import androidx.annotation.RequiresApi +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.Arrangement +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.foundation.layout.width +import androidx.compose.foundation.layout.wrapContentHeight +import androidx.compose.material.Button +import androidx.compose.material.ButtonDefaults +import androidx.compose.material.MaterialTheme +import androidx.compose.material.Scaffold +import androidx.compose.material.SnackbarHost +import androidx.compose.material.SnackbarHostState +import androidx.compose.material.Surface +import androidx.compose.material.Text +import androidx.compose.material.TopAppBar +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.LaunchedEffect +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.platform.LocalContext +import androidx.compose.ui.platform.LocalLifecycleOwner +import androidx.compose.ui.res.colorResource +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.viewmodel.compose.viewModel +import com.google.accompanist.permissions.ExperimentalPermissionsApi +import com.google.accompanist.permissions.isGranted +import com.google.accompanist.permissions.rememberPermissionState +import com.google.firebase.quickstart.fcm.R +import com.google.firebase.quickstart.fcm.kotlin.data.SubscriptionState +import com.google.firebase.quickstart.fcm.kotlin.ui.theme.FirebaseMessagingTheme +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch + +class ComposeMainActivity : ComponentActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContent { + FirebaseMessagingTheme { + // A surface container using the 'background' color from the theme + Surface( + modifier = Modifier.fillMaxSize(), + color = MaterialTheme.colors.background + ) { + MainAppView() + } + } + } + } +} + + +@OptIn(ExperimentalPermissionsApi::class) +@RequiresApi(Build.VERSION_CODES.TIRAMISU) +@Composable +private fun RequestNotificationPermissionDialog( + scope: CoroutineScope, + snackbarHostState: SnackbarHostState +) { + val permissionState = rememberPermissionState(permission = Manifest.permission.POST_NOTIFICATIONS) + + val requestPermissionLauncher = rememberLauncherForActivityResult( + ActivityResultContracts.RequestPermission() + ) { isGranted -> + var message = "Notifications permission granted" + if (!isGranted) { + message = "FCM can't post notifications without POST_NOTIFICATIONS permission" + } + + scope.launch { + snackbarHostState.showSnackbar(message) + } + } + + LaunchedEffect(permissionState) { + if (!permissionState.status.isGranted) { + requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) + } + } +} + +@Composable +fun MainAppView( + lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, + fcmViewModel: FirebaseMessagingViewModel = viewModel(factory = FirebaseMessagingViewModel.Factory) +) { + val context = LocalContext.current + val scope = rememberCoroutineScope() + val snackbarHostState = remember { SnackbarHostState() } + + val activity = context.findActivity() + val intent = activity?.intent + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + RequestNotificationPermissionDialog(scope, snackbarHostState) + } + + DisposableEffect(lifecycleOwner) { + val observer = LifecycleEventObserver { _, event -> + if (event == Lifecycle.Event.ON_CREATE) { + //Create Notification Channel + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + fcmViewModel.setNotificationChannel( + context, + context.getString(R.string.default_notification_channel_id), + context.getString(R.string.default_notification_channel_name) + ) + } + + if (intent != null) { + fcmViewModel.getNotificationData(intent) + } + } + } + + lifecycleOwner.lifecycle.addObserver(observer) + + onDispose { + lifecycleOwner.lifecycle.removeObserver(observer) + } + } + + LaunchedEffect(key1 = fcmViewModel.token) { + fcmViewModel.token.collect { + if(it.isNotEmpty()) { + snackbarHostState.showSnackbar(context.getString(R.string.msg_token_fmt, it)) + } + } + } + + LaunchedEffect(key1 = fcmViewModel.subscriptionState) { + fcmViewModel.subscriptionState.collect { state -> + when (state) { + SubscriptionState.Success -> { snackbarHostState.showSnackbar(context.getString(R.string.msg_subscribed)) } + SubscriptionState.Failed -> { snackbarHostState.showSnackbar(context.getString(R.string.msg_subscribe_failed)) } + SubscriptionState.Loading -> { } + } + } + } + + Scaffold( + snackbarHost = { + SnackbarHost(hostState = snackbarHostState) + }, + modifier = Modifier + .fillMaxSize(), + topBar = { + TopAppBar( + backgroundColor = colorResource(R.color.colorPrimary) + ) { + Text( + text = stringResource(R.string.app_name), + style = MaterialTheme.typography.h6, + textAlign = TextAlign.Center, + modifier = Modifier.padding(8.dp), + color = Color.White + ) + } + }, + content = { it -> + Column( + modifier = Modifier + .fillMaxSize() + .padding(it) + ) { + MainContent(fcmViewModel) + } + } + ) +} + +@Composable +fun MainContent( + fcmViewModel: FirebaseMessagingViewModel +) { + Column( + modifier = Modifier + .fillMaxSize() + .padding(16.dp), + verticalArrangement = Arrangement.Top, + horizontalAlignment = Alignment.CenterHorizontally + ) { + Image( + painter = painterResource(R.drawable.firebase_lockup_400), + contentDescription = "Firebase logo", + modifier = Modifier.fillMaxWidth(), + ) + Text( + text = stringResource(R.string.quickstart_message), + textAlign = TextAlign.Center, + style = MaterialTheme.typography.body1, + modifier = Modifier + .padding(top = 16.dp, start=8.dp, end=8.dp, bottom=16.dp) + ) + Button( + modifier = Modifier + .width(200.dp) + .wrapContentHeight() + .padding(0.dp, 20.dp, 0.dp, 0.dp), + colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(R.color.colorPrimary)), + onClick = { + fcmViewModel.getSubscribe("weather") + } + ) { + Text( + textAlign = TextAlign.Center, + text = stringResource(R.string.subscribe_to_weather).uppercase(), + color = Color.White, + ) + } + Button( + modifier = Modifier + .width(200.dp) + .wrapContentHeight() + .padding(0.dp, 20.dp, 0.dp, 0.dp), + colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(R.color.colorPrimary)), + onClick = { + fcmViewModel.getToken() + } + ) { + Text( + text = stringResource(R.string.log_token).uppercase(), + color = Color.White, + ) + } + } +} + +@Preview(showBackground = true) +@Composable +fun GreetingPreview() { + FirebaseMessagingTheme { + MainAppView() + } +} + +fun Context.findActivity(): Activity? = when (this) { + is Activity -> this + is ContextWrapper -> baseContext.findActivity() + else -> null +} \ No newline at end of file diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/FirebaseMessagingViewModel.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/FirebaseMessagingViewModel.kt new file mode 100644 index 000000000..e47ffe4d4 --- /dev/null +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/FirebaseMessagingViewModel.kt @@ -0,0 +1,100 @@ +package com.google.firebase.quickstart.fcm.kotlin + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context +import android.content.Intent +import android.os.Build +import android.util.Log +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.CreationExtras +import com.google.android.gms.tasks.OnCompleteListener +import com.google.firebase.messaging.FirebaseMessaging +import com.google.firebase.quickstart.fcm.kotlin.data.SubscriptionState +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch + +class FirebaseMessagingViewModel( + private val messaging: FirebaseMessaging +): ViewModel() { + + private var _token: MutableStateFlow = MutableStateFlow("") + val token: MutableStateFlow = _token + + private var _subscriptionState = MutableStateFlow(SubscriptionState.Loading) + val subscriptionState: StateFlow = _subscriptionState + + fun setNotificationChannel(context: Context, channelId: String, channelName: String ) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // Create channel to show notifications. + context.getSystemService(NotificationManager::class.java)?.createNotificationChannel( + NotificationChannel( + channelId, + channelName, + NotificationManager.IMPORTANCE_LOW, + ), + ) + } + } + + fun getNotificationData(intent: Intent) { + viewModelScope.launch { + intent.extras?.let { + it.keySet().forEach { key -> + val value = intent.extras?.getString(key) + Log.d(TAG, "Key: $key Value: $value") + } + } + } + } + + fun getToken() { + viewModelScope.launch { + messaging.getToken().addOnCompleteListener(OnCompleteListener { task -> + if (!task.isSuccessful) { + Log.w(TAG, "Fetching FCM registration token failed", task.exception) + return@OnCompleteListener + } + + _token.value = task.result + Log.d(TAG, "FCM registration Token: " + task.result) + }) + } + } + + fun getSubscribe(topicName: String) { + viewModelScope.launch { + Log.d(TAG, "Subscribing to topic: $topicName") + _subscriptionState.value = SubscriptionState.Loading + messaging.subscribeToTopic(topicName) + .addOnCompleteListener { task -> + _subscriptionState.value = when { + !task.isSuccessful -> SubscriptionState.Failed + else -> { + Log.d(TAG, "Subscribed to weather topic") + SubscriptionState.Success + } + } + } + } + } + + companion object { + const val TAG = "FCMViewModel" + // 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 create( + modelClass: Class, + extras: CreationExtras + ): T { + val firebaseMessaging = FirebaseMessaging.getInstance() + return FirebaseMessagingViewModel(firebaseMessaging) as T + } + } + } +} \ No newline at end of file diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt index 2db8171f5..bb52b8a7b 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt @@ -1,23 +1,23 @@ package com.google.firebase.quickstart.fcm.kotlin import android.Manifest -import android.app.NotificationChannel -import android.app.NotificationManager import android.content.pm.PackageManager import android.os.Build import android.os.Bundle -import android.util.Log import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts +import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat -import com.google.android.gms.tasks.OnCompleteListener -import com.google.firebase.ktx.Firebase -import com.google.firebase.messaging.ktx.messaging +import androidx.lifecycle.lifecycleScope +import com.google.android.material.snackbar.Snackbar import com.google.firebase.quickstart.fcm.R import com.google.firebase.quickstart.fcm.databinding.ActivityMainBinding +import com.google.firebase.quickstart.fcm.kotlin.data.SubscriptionState +import kotlinx.coroutines.launch class MainActivity : AppCompatActivity() { + private val viewModel: FirebaseMessagingViewModel by viewModels { FirebaseMessagingViewModel.Factory } private val requestPermissionLauncher = registerForActivityResult( ActivityResultContracts.RequestPermission() @@ -38,11 +38,11 @@ class MainActivity : AppCompatActivity() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Create channel to show notifications. - val channelId = getString(R.string.default_notification_channel_id) - val channelName = getString(R.string.default_notification_channel_name) - val notificationManager = getSystemService(NotificationManager::class.java) - notificationManager?.createNotificationChannel(NotificationChannel(channelId, - channelName, NotificationManager.IMPORTANCE_LOW)) + viewModel.setNotificationChannel( + this, + getString(R.string.default_notification_channel_id), + getString(R.string.default_notification_channel_name) + ) } // If a notification message is tapped, any data accompanying the notification @@ -54,49 +54,46 @@ class MainActivity : AppCompatActivity() { // // Handle possible data accompanying notification message. // [START handle_data_extras] - intent.extras?.let { - for (key in it.keySet()) { - val value = intent.extras?.get(key) - Log.d(TAG, "Key: $key Value: $value") - } - } + viewModel.getNotificationData(intent) // [END handle_data_extras] binding.subscribeButton.setOnClickListener { - Log.d(TAG, "Subscribing to weather topic") // [START subscribe_topics] - Firebase.messaging.subscribeToTopic("weather") - .addOnCompleteListener { task -> - var msg = getString(R.string.msg_subscribed) - if (!task.isSuccessful) { - msg = getString(R.string.msg_subscribe_failed) - } - Log.d(TAG, msg) - Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() - } + viewModel.getSubscribe("weather") // [END subscribe_topics] } binding.logTokenButton.setOnClickListener { // Get token // [START log_reg_token] - Firebase.messaging.getToken().addOnCompleteListener(OnCompleteListener { task -> - if (!task.isSuccessful) { - Log.w(TAG, "Fetching FCM registration token failed", task.exception) - return@OnCompleteListener - } - - // Get new FCM registration token - val token = task.result - - // Log and toast - val msg = getString(R.string.msg_token_fmt, token) - Log.d(TAG, msg) - Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() - }) + viewModel.getToken() // [END log_reg_token] } + lifecycleScope.launch { + viewModel.token.collect { token -> + if(token.isNotEmpty()){ + val msg = getString(R.string.msg_token_fmt, token) + Snackbar.make(findViewById(android.R.id.content), + msg, Snackbar.LENGTH_LONG).show() + } + } + } + + lifecycleScope.launch { + viewModel.subscriptionState.collect { state -> + when (state) { + SubscriptionState.Success -> { + Snackbar.make(findViewById(android.R.id.content), + getString(R.string.msg_subscribed), Snackbar.LENGTH_LONG).show() + } + SubscriptionState.Failed -> { Snackbar.make(findViewById(android.R.id.content), + getString(R.string.msg_subscribe_failed), Snackbar.LENGTH_LONG).show() + } + SubscriptionState.Loading -> { } + } + } + } Toast.makeText(this, "See README for setup instructions", Toast.LENGTH_SHORT).show() askNotificationPermission() } @@ -116,7 +113,6 @@ class MainActivity : AppCompatActivity() { } companion object { - private const val TAG = "MainActivity" } } diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt index 4828253c2..0d805bc46 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt @@ -8,6 +8,7 @@ import android.content.Intent import android.media.RingtoneManager import android.os.Build import android.util.Log +import androidx.compose.material.ExperimentalMaterialApi import androidx.core.app.NotificationCompat import androidx.work.OneTimeWorkRequest import androidx.work.WorkManager @@ -113,6 +114,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() { * * @param messageBody FCM message body received. */ + @OptIn(ExperimentalMaterialApi::class) private fun sendNotification(messageBody: String) { val intent = Intent(this, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/data/SubscriptionState.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/data/SubscriptionState.kt new file mode 100644 index 000000000..b50466f43 --- /dev/null +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/data/SubscriptionState.kt @@ -0,0 +1,5 @@ +package com.google.firebase.quickstart.fcm.kotlin.data + +enum class SubscriptionState { + Success, Failed, Loading +} \ No newline at end of file diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Color.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Color.kt new file mode 100644 index 000000000..20343e40c --- /dev/null +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package com.google.firebase.quickstart.fcm.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 \ No newline at end of file diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Shape.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Shape.kt new file mode 100644 index 000000000..e9c74dea9 --- /dev/null +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Shape.kt @@ -0,0 +1,11 @@ +package com.google.firebase.quickstart.fcm.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) +) \ No newline at end of file diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Theme.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Theme.kt new file mode 100644 index 000000000..c0284b207 --- /dev/null +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Theme.kt @@ -0,0 +1,38 @@ +package com.google.firebase.quickstart.fcm.kotlin.ui.theme + +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material.MaterialTheme +import androidx.compose.material.darkColors +import androidx.compose.material.lightColors +import androidx.compose.runtime.Composable + +private val DarkColorPalette = darkColors( + primary = Purple80, + primaryVariant = PurpleGrey80, + secondary = Pink80 +) + +private val LightColorPalette = lightColors( + primary = FirebaseBlue, + primaryVariant = FirebaseBannerBlue, + secondary = FirebaseOrange +) + +@Composable +fun FirebaseMessagingTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + content: @Composable () -> Unit +) { + val colors = if (darkTheme) { + DarkColorPalette + } else { + LightColorPalette + } + + MaterialTheme( + colors = colors, + typography = Typography, + shapes = Shapes, + content = content + ) +} \ No newline at end of file diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Type.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Type.kt new file mode 100644 index 000000000..355ec16f6 --- /dev/null +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ui/theme/Type.kt @@ -0,0 +1,18 @@ +package com.google.firebase.quickstart.fcm.kotlin.ui.theme + +import androidx.compose.material.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + body1 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) +) \ No newline at end of file diff --git a/messaging/gradle.properties b/messaging/gradle.properties index aac7c9b46..29b531a1d 100644 --- a/messaging/gradle.properties +++ b/messaging/gradle.properties @@ -10,7 +10,7 @@ # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. org.gradle.jvmargs=-Xmx1536m - +android.useAndroidX=true # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects From a89205d7505644c043d5901ce2a1552df06e8481 Mon Sep 17 00:00:00 2001 From: lehcar09 Date: Wed, 19 Jun 2024 02:19:00 +0800 Subject: [PATCH 3/5] - Update compose preview --- .../firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt index 33aba6589..4b6340a51 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt @@ -254,7 +254,7 @@ fun MainContent( @Preview(showBackground = true) @Composable -fun GreetingPreview() { +fun MainAppViewPreview() { FirebaseMessagingTheme { MainAppView() } From 9d3303980942c17df0a2c01f9355d65417a9d066 Mon Sep 17 00:00:00 2001 From: lehcar09 Date: Wed, 21 Aug 2024 17:50:52 +0800 Subject: [PATCH 4/5] Resolve review comments/ suggestions --- .../fcm/kotlin/ComposeMainActivity.kt | 73 +++++++------------ .../fcm/kotlin/FirebaseMessagingViewModel.kt | 67 +++++++++-------- .../quickstart/fcm/kotlin/MainActivity.kt | 30 ++------ .../fcm/kotlin/MyFirebaseMessagingService.kt | 2 - messaging/app/src/main/res/values/strings.xml | 5 ++ 5 files changed, 77 insertions(+), 100 deletions(-) diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt index 4b6340a51..0a938f356 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/ComposeMainActivity.kt @@ -1,6 +1,5 @@ package com.google.firebase.quickstart.fcm.kotlin -import android.Manifest import android.app.Activity import android.content.Context import android.content.ContextWrapper @@ -10,7 +9,6 @@ import androidx.activity.ComponentActivity import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.setContent import androidx.activity.result.contract.ActivityResultContracts -import androidx.annotation.RequiresApi import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -48,13 +46,9 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.viewmodel.compose.viewModel -import com.google.accompanist.permissions.ExperimentalPermissionsApi -import com.google.accompanist.permissions.isGranted -import com.google.accompanist.permissions.rememberPermissionState import com.google.firebase.quickstart.fcm.R import com.google.firebase.quickstart.fcm.kotlin.data.SubscriptionState import com.google.firebase.quickstart.fcm.kotlin.ui.theme.FirebaseMessagingTheme -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch class ComposeMainActivity : ComponentActivity() { @@ -68,29 +62,31 @@ class ComposeMainActivity : ComponentActivity() { modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background ) { - MainAppView() + MainScreen() } } } } } - -@OptIn(ExperimentalPermissionsApi::class) -@RequiresApi(Build.VERSION_CODES.TIRAMISU) @Composable -private fun RequestNotificationPermissionDialog( - scope: CoroutineScope, - snackbarHostState: SnackbarHostState +fun MainScreen( + lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, + fcmViewModel: FirebaseMessagingViewModel = viewModel(factory = FirebaseMessagingViewModel.Factory) ) { - val permissionState = rememberPermissionState(permission = Manifest.permission.POST_NOTIFICATIONS) + val context = LocalContext.current + val scope = rememberCoroutineScope() + val snackbarHostState = remember { SnackbarHostState() } + + val activity = context.findActivity() + val intent = activity?.intent val requestPermissionLauncher = rememberLauncherForActivityResult( ActivityResultContracts.RequestPermission() ) { isGranted -> - var message = "Notifications permission granted" + var message = context.getString(R.string.msg_permission_granted) if (!isGranted) { - message = "FCM can't post notifications without POST_NOTIFICATIONS permission" + message = context.getString(R.string.msg_permission_failed) } scope.launch { @@ -98,29 +94,6 @@ private fun RequestNotificationPermissionDialog( } } - LaunchedEffect(permissionState) { - if (!permissionState.status.isGranted) { - requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) - } - } -} - -@Composable -fun MainAppView( - lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, - fcmViewModel: FirebaseMessagingViewModel = viewModel(factory = FirebaseMessagingViewModel.Factory) -) { - val context = LocalContext.current - val scope = rememberCoroutineScope() - val snackbarHostState = remember { SnackbarHostState() } - - val activity = context.findActivity() - val intent = activity?.intent - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - RequestNotificationPermissionDialog(scope, snackbarHostState) - } - DisposableEffect(lifecycleOwner) { val observer = LifecycleEventObserver { _, event -> if (event == Lifecycle.Event.ON_CREATE) { @@ -133,9 +106,15 @@ fun MainAppView( ) } - if (intent != null) { - fcmViewModel.getNotificationData(intent) + intent?.let { intentData -> + fcmViewModel.logNotificationData(intentData) } + + scope.launch { + snackbarHostState.showSnackbar(context.getString(R.string.msg_setup_readme_instructions)) + } + + fcmViewModel.askNotificationPermission(context, requestPermissionLauncher) } } @@ -146,7 +125,7 @@ fun MainAppView( } } - LaunchedEffect(key1 = fcmViewModel.token) { + LaunchedEffect(fcmViewModel.token) { fcmViewModel.token.collect { if(it.isNotEmpty()) { snackbarHostState.showSnackbar(context.getString(R.string.msg_token_fmt, it)) @@ -154,7 +133,7 @@ fun MainAppView( } } - LaunchedEffect(key1 = fcmViewModel.subscriptionState) { + LaunchedEffect(fcmViewModel.subscriptionState) { fcmViewModel.subscriptionState.collect { state -> when (state) { SubscriptionState.Success -> { snackbarHostState.showSnackbar(context.getString(R.string.msg_subscribed)) } @@ -183,11 +162,11 @@ fun MainAppView( ) } }, - content = { it -> + content = { innerPadding -> Column( modifier = Modifier .fillMaxSize() - .padding(it) + .padding(innerPadding) ) { MainContent(fcmViewModel) } @@ -225,7 +204,7 @@ fun MainContent( .padding(0.dp, 20.dp, 0.dp, 0.dp), colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(R.color.colorPrimary)), onClick = { - fcmViewModel.getSubscribe("weather") + fcmViewModel.subscribeToTopic("weather") } ) { Text( @@ -256,7 +235,7 @@ fun MainContent( @Composable fun MainAppViewPreview() { FirebaseMessagingTheme { - MainAppView() + MainScreen() } } diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/FirebaseMessagingViewModel.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/FirebaseMessagingViewModel.kt index e47ffe4d4..2e632719b 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/FirebaseMessagingViewModel.kt +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/FirebaseMessagingViewModel.kt @@ -1,21 +1,25 @@ package com.google.firebase.quickstart.fcm.kotlin +import android.Manifest import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import android.content.Intent +import android.content.pm.PackageManager import android.os.Build import android.util.Log +import androidx.activity.result.ActivityResultLauncher +import androidx.core.content.ContextCompat import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewmodel.CreationExtras -import com.google.android.gms.tasks.OnCompleteListener import com.google.firebase.messaging.FirebaseMessaging import com.google.firebase.quickstart.fcm.kotlin.data.SubscriptionState import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch +import kotlinx.coroutines.tasks.await class FirebaseMessagingViewModel( private val messaging: FirebaseMessaging @@ -27,6 +31,19 @@ class FirebaseMessagingViewModel( private var _subscriptionState = MutableStateFlow(SubscriptionState.Loading) val subscriptionState: StateFlow = _subscriptionState + fun askNotificationPermission( + context: Context, + requestPermissionLauncher: ActivityResultLauncher + ) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == + PackageManager.PERMISSION_DENIED + ) { + requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) + } + } + } + fun setNotificationChannel(context: Context, channelId: String, channelName: String ) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Create channel to show notifications. @@ -40,45 +57,37 @@ class FirebaseMessagingViewModel( } } - fun getNotificationData(intent: Intent) { - viewModelScope.launch { - intent.extras?.let { - it.keySet().forEach { key -> - val value = intent.extras?.getString(key) - Log.d(TAG, "Key: $key Value: $value") - } + fun logNotificationData(intent: Intent) { + intent.extras?.let { + it.keySet().forEach { key -> + val value = intent.extras?.getString(key) + Log.d(TAG, "Key: $key Value: $value") } } } fun getToken() { viewModelScope.launch { - messaging.getToken().addOnCompleteListener(OnCompleteListener { task -> - if (!task.isSuccessful) { - Log.w(TAG, "Fetching FCM registration token failed", task.exception) - return@OnCompleteListener - } - - _token.value = task.result - Log.d(TAG, "FCM registration Token: " + task.result) - }) + try { + _token.value = messaging.getToken().await() + Log.d(TAG, "FCM registration Token: ${_token.value}") + } catch(e: Exception) { + Log.w(TAG, "Fetching FCM registration token failed", e) + } } } - fun getSubscribe(topicName: String) { + fun subscribeToTopic(topicName: String) { viewModelScope.launch { Log.d(TAG, "Subscribing to topic: $topicName") - _subscriptionState.value = SubscriptionState.Loading - messaging.subscribeToTopic(topicName) - .addOnCompleteListener { task -> - _subscriptionState.value = when { - !task.isSuccessful -> SubscriptionState.Failed - else -> { - Log.d(TAG, "Subscribed to weather topic") - SubscriptionState.Success - } - } - } + try { + messaging.subscribeToTopic(topicName).await() + Log.d(TAG, "Subscribed to $topicName topic") + _subscriptionState.value = SubscriptionState.Success + } catch (e: Exception) { + Log.w(TAG, "Failed to subscribe to $topicName", e) + _subscriptionState.value = SubscriptionState.Failed + } } } diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt index bb52b8a7b..c85adc032 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt @@ -1,14 +1,11 @@ package com.google.firebase.quickstart.fcm.kotlin -import android.Manifest -import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity -import androidx.core.content.ContextCompat import androidx.lifecycle.lifecycleScope import com.google.android.material.snackbar.Snackbar import com.google.firebase.quickstart.fcm.R @@ -54,17 +51,16 @@ class MainActivity : AppCompatActivity() { // // Handle possible data accompanying notification message. // [START handle_data_extras] - viewModel.getNotificationData(intent) + viewModel.logNotificationData(intent) // [END handle_data_extras] binding.subscribeButton.setOnClickListener { // [START subscribe_topics] - viewModel.getSubscribe("weather") + viewModel.subscribeToTopic("weather") // [END subscribe_topics] } binding.logTokenButton.setOnClickListener { - // Get token // [START log_reg_token] viewModel.getToken() // [END log_reg_token] @@ -87,28 +83,18 @@ class MainActivity : AppCompatActivity() { Snackbar.make(findViewById(android.R.id.content), getString(R.string.msg_subscribed), Snackbar.LENGTH_LONG).show() } - SubscriptionState.Failed -> { Snackbar.make(findViewById(android.R.id.content), - getString(R.string.msg_subscribe_failed), Snackbar.LENGTH_LONG).show() + SubscriptionState.Failed -> { + Snackbar.make(findViewById(android.R.id.content), + getString(R.string.msg_subscribe_failed), Snackbar.LENGTH_LONG).show() } SubscriptionState.Loading -> { } } } } - Toast.makeText(this, "See README for setup instructions", Toast.LENGTH_SHORT).show() - askNotificationPermission() - } + Toast.makeText(this, getString(R.string.msg_setup_readme_instructions), Toast.LENGTH_SHORT).show() - private fun askNotificationPermission() { - // This is only necessary for API Level > 33 (TIRAMISU) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == - PackageManager.PERMISSION_GRANTED - ) { - // FCM SDK (and your app) can post notifications. - } else { - // Directly ask for the permission - requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) - } + lifecycleScope.launch { + viewModel.askNotificationPermission(applicationContext, requestPermissionLauncher) } } diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt index 0d805bc46..4828253c2 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt @@ -8,7 +8,6 @@ import android.content.Intent import android.media.RingtoneManager import android.os.Build import android.util.Log -import androidx.compose.material.ExperimentalMaterialApi import androidx.core.app.NotificationCompat import androidx.work.OneTimeWorkRequest import androidx.work.WorkManager @@ -114,7 +113,6 @@ class MyFirebaseMessagingService : FirebaseMessagingService() { * * @param messageBody FCM message body received. */ - @OptIn(ExperimentalMaterialApi::class) private fun sendNotification(messageBody: String) { val intent = Intent(this, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) diff --git a/messaging/app/src/main/res/values/strings.xml b/messaging/app/src/main/res/values/strings.xml index aa1617c3e..633f6c52b 100644 --- a/messaging/app/src/main/res/values/strings.xml +++ b/messaging/app/src/main/res/values/strings.xml @@ -19,4 +19,9 @@ Failed to subscribe to weather topic FCM Message + + Notifications permission granted + FCM can\'t post notifications without POST_NOTIFICATIONS permission + + See README for setup instructions" From 4eba4a94adb8d5484c90ed73e65b8bbc8cefe12a Mon Sep 17 00:00:00 2001 From: lehcar09 Date: Tue, 3 Sep 2024 16:20:02 +0800 Subject: [PATCH 5/5] Remove accompanist dependency --- messaging/app/build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/messaging/app/build.gradle b/messaging/app/build.gradle index 777037c86..efe533230 100644 --- a/messaging/app/build.gradle +++ b/messaging/app/build.gradle @@ -98,7 +98,6 @@ dependencies { implementation 'androidx.activity:activity-compose:1.5.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0' - implementation("com.google.accompanist:accompanist-permissions:0.30.1") // Testing dependencies androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'