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

Kotlin中Firebase推送通知点击后如何跳转对应不同的Activity

实现方案

核心思路

给不同类型的推送添加自定义标识参数,在接收推送时动态判断标识,生成对应目标Activity的跳转Intent,替代原有的硬编码跳转逻辑。

具体实现步骤

步骤1:推送携带自定义标识

发送Firebase推送时,在data字段中添加专门的跳转标识参数(比如target_type),也可以直接复用已有的message字段做判断,更推荐新增独立参数方便后续维护:

  • 需要跳转Activity1的推送,data中携带"target_type": "activity1" 或者message内容为text X
  • 需要跳转Activity3的推送,data中携带"target_type": "activity3" 或者message内容为text Z

步骤2:修改FirebaseService的跳转逻辑

修改onMessageReceived方法中硬编码的Intent生成逻辑,根据拿到的标识动态匹配目标Activity:

private const val CHANNEL_ID = "my_channel"

 class FirebaseService: FirebaseMessagingService() {

    companion object {
        var sharedPref: SharedPreferences? = null

        var token: String?
            get() {
                return sharedPref?.getString("token", "")
            }
            set(value) {
                sharedPref?.edit()?.putString("token", value)?.apply()
            }
    }

    override fun onNewToken(newToken: String) {
        super.onNewToken(newToken)
        token = newToken
    }

    override fun onMessageReceived(message: RemoteMessage) {
        super.onMessageReceived(message)

        // 1. 取跳转标识,这里示例同时支持判断target_type参数和message内容
        val targetType = message.data["target_type"] ?: message.data["message"].orEmpty()
        // 2. 动态匹配目标Activity
        val targetActivity = when(targetType) {
            "activity1", "text X" -> Activity1::class.java
            "activity3", "text Z" -> Activity3::class.java
            else -> SearchActivity::class.java
        }
        // 3. 生成对应Intent,还可以按需把推送的其他参数传给目标Activity
        val intent = Intent(this, targetActivity).apply {
            putExtra("push_title", message.data["title"])
            putExtra("push_content", message.data["message"])
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        }
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notificationID = Random.nextInt()

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel(notificationManager)
        }
        // 高版本安卓需要额外加FLAG_IMMUTABLE或FLAG_MUTABLE参数避免崩溃
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
        val notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle(message.data["title"])
            .setContentText(message.data["message"])
            .setSmallIcon(R.drawable.gaz)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build()

        notificationManager.notify(notificationID, notification)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel(notificationManager: NotificationManager) {
        val channelName = "channelName"
        val channel = NotificationChannel(CHANNEL_ID, channelName, IMPORTANCE_HIGH).apply {
            description = "My channel description"
            enableLights(true)
            lightColor = Color.GREEN
        }
        notificationManager.createNotificationChannel(channel)
    }
}

注意事项

当应用处于后台/被杀死状态时,Firebase推送默认由系统通知栏处理,不会触发onMessageReceived方法,此时点击通知会直接打开应用的启动页。如果需要这类场景下也能正常跳转对应页面,你需要在应用启动页的onCreate/onNewIntent方法中解析intent携带的推送参数,再根据参数二次跳转到目标Activity。

内容的提问来源于stack exchange,提问作者vad070

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 13:24:03