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

Firebase通知跳转异常:后台收通知后恢复应用自动跳转指定页面

问题解决方案

问题根源

你的代码中handleIntent方法会把所有收到的Intent都转换为RemoteMessage并调用onMessageReceived,包括应用从后台恢复时的系统触发Intent,这导致后台收到通知后,应用恢复时会重复执行通知跳转逻辑。同时,当前的PendingIntent没有区分"通知点击"和"应用正常恢复"的场景。

修改步骤

1. 给跳转Intent添加点击标识

在sendNotification方法中,给跳转Intent添加一个专属参数,标记该Intent来自通知点击:

private fun sendNotification(message: RemoteMessage) {
    val title = message.notification?.title.orEmpty()
    val body = message.notification?.body.orEmpty()

    val notificationIntent: Intent = if (message.notification?.clickAction == "TEST_ACTION") {
        Intent(this, RecognizeUserActivity::class.java).apply {
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
            putExtra("message", body)
            putExtra("FROM_NOTIFICATION_CLICK", true) // 添加点击标识
        }
    } else {
        Intent(this, MainActivity::class.java).apply {
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
            putExtra("message", body)
            putExtra("FROM_NOTIFICATION_CLICK", true) // 添加点击标识
        }
    }

    // 剩余代码保持不变...
}

2. 修正handleIntent逻辑

避免在非通知点击场景下重复执行onMessageReceived:

override fun handleIntent(intent: Intent) {
    val isFromNotificationClick = intent.getBooleanExtra("FROM_NOTIFICATION_CLICK", false)
    if (!isFromNotificationClick) {
        // 仅处理数据类型消息,避免干扰系统通知逻辑
        if (intent.extras?.containsKey("data") == true) {
            val builder = RemoteMessage.Builder("FirebaseMessagingService")
            intent.extras?.keySet()?.forEach { key ->
                builder.addData(key, intent.extras?.getString(key))
            }
            onMessageReceived(builder.build())
        } else {
            super.handleIntent(intent)
        }
    } else {
        // 通知点击的Intent直接交给系统处理启动Activity
        super.handleIntent(intent)
    }
}

3. 在目标Activity中校验标识

在RecognizeUserActivity中添加校验逻辑,只有来自通知点击的请求才保留当前页面:

class RecognizeUserActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recognize_user)

        val fromNotification = intent.getBooleanExtra("FROM_NOTIFICATION_CLICK", false)
        if (!fromNotification) {
            // 非通知点击场景,跳转回主页面
            startActivity(Intent(this, MainActivity::class.java))
            finish()
            return
        }

        // 处理通知携带的消息
        val message = intent.getStringExtra("message")
        // 后续业务逻辑...
    }

    // 适配FLAG_ACTIVITY_SINGLE_TOP模式下的Intent更新
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        val fromNotification = intent?.getBooleanExtra("FROM_NOTIFICATION_CLICK", false) ?: false
        if (!fromNotification) {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
            return
        }
        // 更新消息处理逻辑...
    }
}

原理说明

  • 通过FROM_NOTIFICATION_CLICK参数精准区分"用户主动点击通知"和"应用后台恢复"两种场景
  • 修正handleIntent的逻辑,避免系统触发的Intent重复执行通知创建逻辑
  • 在目标Activity中做最终校验,确保只有点击通知才会停留在目标页面

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 00:17:06