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

Android应用后台状态下通知PendingIntent点击无响应如何解决?

Firebase推送后台点击通知无响应问题解决

问题现象

发送推送通知时,若应用处于后台状态可正常收到通知,但点击通知无任何响应;若应用处于前台状态,点击通知可正常跳转回应用。

原有问题代码

FirebaseMessagingService中的实现如下:

remoteMessage.notification.let {
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        val intent = Intent(this, MainActivity::class.java)
                .putExtra(CLICK_ACTION, mapData.get("click_action"))
                .putExtra(PAGE_TO_GO, mapData.get("pagetogo"))
                .putExtra(NOTIFIATION_TITLE, mapData.get("title"))
                .putExtra(NOTIFICATION_BODY, mapData.get("body"))
                .putExtra(COMPANY_ID, mapData.get("company_id"))
                .putExtra(DOCUMENT_ID, mapData.get("document_id"))
                .putExtra(ORDER_ID, mapData.get("order_id"))
                .putExtra(FROM_DATE_EXTRA, mapData.get("from_date"))
                .putExtra(TO_DATE_EXTRA, mapData.get("to_date"))
                .putExtra(LEDGER_VIEW_ID_EXTRA, mapData.get("id_for_ledge_view"))
                .putExtra(IS_CREDIT_EXTRA, mapData.get("is_credit"))
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

        val pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        val notification = NotificationCompat.Builder(this, getString(R.string.channel_id))
            .setSmallIcon(R.drawable.ic_baseline_notifications_24)
            .setContentTitle(it?.title)
            .setContentText(it?.body)
            .setContentIntent(pendingIntent)
            .setStyle(
                NotificationCompat.BigTextStyle()
                    .bigText(it?.body)
            )
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
        with(notificationManager) {
            notify(NOTIFICATION_ID, notification.build())
        }

根因分析

Firebase云消息的默认逻辑中,remoteMessage.notification相关的回调逻辑仅会在应用处于前台状态时触发。当应用退居后台时,通知会直接由系统渲染生成,不会执行上述自定义的通知构建代码,因此代码中绑定的跳转PendingIntent不会生效,导致点击通知无响应。

解决方案

直接移除remoteMessage.notification.let {这层包裹逻辑,改为统一处理所有推送消息的通知构建流程即可,修改后前后台点击通知的跳转逻辑都可以正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 16:15:00