如何通过 Jetpack Compose 中的通知导航到可组合项

问题描述 投票:0回答:1

我正在尝试在单击我发送的通知后将用户导航到屏幕。关于这个主题的文章有限。我尝试过,但无法完成该操作。我的代码:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberNavController()

            NavHost(navController = navController, startDestination = "main") {
                composable("main") {
                    main(this@MainActivity)
                }
                composable("testScreen") {
                    testScreen()
                }
            }

        }
    }
}

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun main(mainActivity: MainActivity) {
    val postNotificationPermission = rememberPermissionState(permission = Manifest.permission.POST_NOTIFICATIONS)
    val waterNotificationService = WaterNotificationService(mainActivity)


    LaunchedEffect(Unit) {
        if (!postNotificationPermission.status.isGranted) {
            postNotificationPermission.launchPermissionRequest()
        }
    }

    Column {
        Button(onClick = {
            waterNotificationService.showBasicNotification()
        }) {
            Text("SHOW NOTIFICATION")

        }
    }
}

@Composable
fun testScreen() {
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text("TEST SCREEN WELCOME")
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

    <application
        android:name=".WaterApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.NotificationNavigation"
        tools:targetApi="31">
        <receiver android:name="com.yunusulucay.healthylife.AlarmSetup.AlarmReceiverBreath"/>
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.NotificationNavigation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ResultActivity"
            android:parentActivityName=".MainActivity"/>
    </application>

</manifest>

在这里我只想做的是在单击按钮后它会显示一些通知,在单击该通知后我希望它导航到 testScreen() 可组合项。如果你能给我一个简单的例子,那就更好了。 预先感谢。

android kotlin navigation android-jetpack-compose
1个回答
0
投票

您必须为通知设置actionIntent和pendingIntent,然后您必须根据活动的生命周期在

onCreate
onNewIntent
中获取意图额外内容。

类似这样的:

val activityActionIntent = Intent(context, YourActivity::class.java).apply {
            this.putExtra("testingIntent", true)
            flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        }
        val pendingIntentFlag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else PendingIntent.FLAG_UPDATE_CURRENT
        val activityActionPendingIntent: PendingIntent = PendingIntent.getActivity(
            context,
            requestCode,
            activityActionIntent,
            pendingIntentFlag
        )
        val nBuilder: NotificationCompat.Builder =
            NotificationCompat.Builder(context, "notificationChannel")
                .setSmallIcon(logo)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setColor(ContextCompat.getColor(context, R.color.black))
                .setContentIntent(activityActionPendingIntent)

        notificationManager.notify(REMINDER_ID, nBuilder.build())

在 onCreate 中:

    setContent {
            val viewModel = hiltViewModel<HomeViewModel>()
            val navigateToTestView = intent.getBooleanExtra("testIntent", false)
            val navController = rememberNavController()
            val startDestination = if(navigateToTestView) "testScreen" else "main"

            NavHost(navController = navController, startDestination = startDestination) {
                composable("main") {
                    main(this@MainActivity, navigateToTestView)
                }
                composable("testScreen") {
                    testScreen()
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.