You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jetpack Compose中通过通知导航至Composable页面

实现通知点击跳转至Compose指定页面的解决方案

步骤1:完善通知服务,添加跳转意图

首先实现WaterNotificationService类,在创建通知时通过PendingIntent携带导航标记,确保点击通知能触发页面跳转:

class WaterNotificationService(private val context: Context) {
    private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    init {
        // Android 8.0+ 必须创建通知渠道
        val channel = NotificationChannel(
            "BASIC_CHANNEL",
            "基础通知",
            NotificationManager.IMPORTANCE_DEFAULT
        ).apply {
            description = "用于展示基础通知的渠道"
        }
        notificationManager.createNotificationChannel(channel)
    }

    fun showBasicNotification() {
        // 创建跳转Intent,标记导航目标
        val navigateIntent = Intent(context, MainActivity::class.java).apply {
            action = "TO_TEST_SCREEN"
            flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
        }

        val pendingIntent = PendingIntent.getActivity(
            context,
            0,
            navigateIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val notification = NotificationCompat.Builder(context, "BASIC_CHANNEL")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("测试通知")
            .setContentText("点击跳转至测试页面")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .build()

        notificationManager.notify(1001, notification)
    }
}

步骤2:修改MainActivity,处理导航逻辑

在MainActivity中监听启动/唤醒Intent,根据标记触发NavController导航到testScreen:

class MainActivity : ComponentActivity() {
    private val currentIntent = mutableStateOf<Intent?>(null)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        currentIntent.value = intent
        
        setContent {
            val navController = rememberNavController()

            // 监听Intent变化,执行导航
            LaunchedEffect(currentIntent.value) {
                currentIntent.value?.let { intent ->
                    if (intent.action == "TO_TEST_SCREEN") {
                        navController.navigate("testScreen")
                        // 重置动作,避免重复触发导航
                        intent.action = null
                        currentIntent.value = intent
                    }
                }
            }

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

    // 处理应用后台时收到的Intent
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        currentIntent.value = intent
    }
}

步骤3:确认清单文件配置

你的现有清单文件已满足要求,MainActivity的exported="true"配置正确,无需额外修改。

关键注意点

  • Android 12+ 必须给PendingIntent添加FLAG_IMMUTABLE标记,避免兼容性报错
  • 使用FLAG_ACTIVITY_SINGLE_TOP确保不会重复创建MainActivity实例
  • 通知渠道是Android 8.0+的强制要求,必须提前创建

内容的提问来源于stack exchange,提问作者Yunus Emrah Uluçay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 08:15:04