Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@
android:theme="@style/Theme.PlatformSamples">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.settings.APP_NOTIFICATION_PROMOTION_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<!--required for TFLite/LiteRT style transfer demo -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package com.example.platform.ui.live_updates
import android.annotation.SuppressLint
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -35,13 +37,16 @@ import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState
Expand All @@ -66,15 +71,19 @@ fun LiveUpdateSample() {
.fillMaxSize()
.padding(contentPadding),
) {
NotificationPermission()
Spacer(modifier = Modifier.height(4.dp))
NotificationPostPromotedPermission()
Text(stringResource( R.string.live_update_summary_text))
Spacer(modifier = Modifier.height(4.dp))
NotificationPermission()
Button(onClick = {
onCheckout()
scope.launch {
snackbarHostState.showSnackbar("Order placed")
}
}) {
Button(
onClick = {
onCheckout()
scope.launch {
snackbarHostState.showSnackbar("Order placed")
}
},
) {
Text("Checkout")
}
}
Expand All @@ -100,27 +109,58 @@ fun NotificationPermission() {
notificationPermissionState.launchPermissionRequest()
},
modifier = Modifier
.fillMaxWidth()
.fillMaxWidth(),
permissionStringResourceId = R.string.permission_message,
permissionRationalStringResourceId = R.string.permission_rationale,
)
}
}

@RequiresApi(Build.VERSION_CODES.BAKLAVA)
@Composable
fun NotificationPostPromotedPermission() {
val context = LocalContext.current
val isPostPromotionsEnabled = remember { mutableStateOf(SnackbarNotificationManager.isPostPromotionsEnabled()) }
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
isPostPromotionsEnabled.value = SnackbarNotificationManager.isPostPromotionsEnabled()
}
if (!isPostPromotionsEnabled.value) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and to follow idiomatic Compose conventions, you can use a property delegate (by) for your MutableState. This will allow you to access and modify the value directly without needing to use .value.

To use this, you may need to add import androidx.compose.runtime.getValue and import androidx.compose.runtime.setValue.

Suggested change
val isPostPromotionsEnabled = remember { mutableStateOf(SnackbarNotificationManager.isPostPromotionsEnabled()) }
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
isPostPromotionsEnabled.value = SnackbarNotificationManager.isPostPromotionsEnabled()
}
if (!isPostPromotionsEnabled.value) {
var isPostPromotionsEnabled by remember { mutableStateOf(SnackbarNotificationManager.isPostPromotionsEnabled()) }
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
isPostPromotionsEnabled = SnackbarNotificationManager.isPostPromotionsEnabled()
}
if (!isPostPromotionsEnabled) {

Text(
text = stringResource(R.string.post_promoted_permission_message),
modifier = Modifier.padding(horizontal = 10.dp),
)
Button(
onClick = {
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_PROMOTION_SETTINGS).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using Intent.FLAG_ACTIVITY_NEW_TASK is not ideal here. Since LocalContext.current within a Composable hosted by an Activity will typically be that Activity's context, starting the settings activity in a new task can disrupt the user's navigation flow. When they press the back button from the settings screen, they might not return to your app as expected. It's better to launch the settings activity in the same task by removing this flag.

putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
}
context.startActivity(intent)
},
) {
Text(text = stringResource(R.string.to_settings))
}
}
}

@Composable
private fun NotificationPermissionCard(
shouldShowRationale: Boolean,
onGrantClick: () -> Unit,
modifier: Modifier = Modifier,
permissionStringResourceId: Int,
permissionRationalStringResourceId: Int,
) {
Card(
modifier = modifier,
) {
Text(
text = stringResource(R.string.permission_message),
text = stringResource(permissionStringResourceId),
modifier = Modifier.padding(16.dp),
)
if (shouldShowRationale) {
Text(
text = stringResource(R.string.permission_rationale),
text = stringResource(permissionRationalStringResourceId),
modifier = Modifier.padding(horizontal = 10.dp),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,9 @@ object SnackbarNotificationManager {
}, state.delay)
}
}

@RequiresApi(Build.VERSION_CODES.BAKLAVA)
fun isPostPromotionsEnabled(): Boolean {
return notificationManager.canPostPromotedNotifications()
}
Comment on lines +263 to +265
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The code inside this function is not correctly indented. Following standard Kotlin formatting guidelines improves code readability and maintainability.

Suggested change
fun isPostPromotionsEnabled(): Boolean {
return notificationManager.canPostPromotedNotifications()
}
fun isPostPromotionsEnabled(): Boolean {
return notificationManager.canPostPromotedNotifications()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<string name="permission_grant">Grant</string>
<string name="permission_message">Please grant the notification permission.</string>
<string name="permission_rationale">Notifications are used for order tracking.</string>
<string name="post_promoted_permission_message">Please grant the app the Live Updates notification permission as it is essential to experiencing an enhanced order tracking user experience.</string>
<string name="to_settings">Go to settings</string>
<string name="live_update_summary_text">Clicking the checkout button will simulate the tracking of an order with notifications styled with ProgressStyle.</string>
<string name="checkout">Checkout</string>
<string name="checking_out">Order placed</string>
Expand Down
Loading