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

Android 10及以上版本如何接收Firebase推送时自动启动应用?

解决Android 10+接收Firebase推送无需点击通知打开应用的问题

Android 10(API 29)及以上引入了后台启动限制,普通广播接收器(Broadcast Receiver)无法在后台直接启动Activity,这就是你之前的方案失效的核心原因。以下是可行的解决思路:

核心思路:区分FCM推送类型

FCM推送分为两种类型,不同类型的处理逻辑差异很大:

  • 通知消息(Notification Message):由FCM SDK自动展示通知,只有用户点击后才会触发Intent,无法绕过点击直接启动应用(系统层面强制限制)。
  • 数据消息(Data Message):完全由应用自行处理,不会自动展示通知,你可以在后台Service中接收后,根据应用当前状态决定是否启动Activity。

具体实现步骤

1. 创建自定义Firebase消息服务

继承FirebaseMessagingService,重写onMessageReceived方法处理数据消息:

class CustomFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        // 只处理数据消息
        if (remoteMessage.data.isNotEmpty()) {
            // 解析推送数据(根据业务需求自定义)
            val payload = remoteMessage.data
            // 尝试启动应用
            attemptLaunchApp()
        }
    }

    private fun attemptLaunchApp() {
        val launchIntent = packageManager.getLaunchIntentForPackage(packageName)?.apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        } ?: return

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            when {
                // 应用有正在运行的任务,直接切换到前台
                activityManager.appTasks.isNotEmpty() -> {
                    activityManager.moveTaskToFront(activityManager.appTasks[0].taskId, 0)
                }
                // 应用处于后台但用户最近交互过,尝试启动(可能被系统拦截)
                else -> {
                    // 同时展示高优先级通知作为 fallback,避免启动失败后用户无感知
                    showFallbackNotification()
                    startActivity(launchIntent)
                }
            }
        } else {
            // Android 10及以下版本直接启动
            startActivity(launchIntent)
        }
    }

    private fun showFallbackNotification() {
        val notificationIntent = packageManager.getLaunchIntentForPackage(packageName)?.apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent = PendingIntent.getActivity(
            this,
            0,
            notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val notification = NotificationCompat.Builder(this, "high_priority_channel")
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle("应用提醒")
            .setContentText("点击打开查看详情")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .build()

        NotificationManagerCompat.from(this).notify(1, notification)
    }
}

2. 在Manifest中注册服务

<service
    android:name=".CustomFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

关键注意事项

  • 后台启动限制:Android 10+中,若应用完全被杀死且用户未在最近几分钟内交互,系统会阻止后台启动Activity,这是系统强制政策,无法绕过,此时必须通过通知引导用户点击。
  • 合规性:滥用后台启动会违反Google Play政策,可能导致应用下架,务必确保启动行为符合用户预期。
  • 前台Service场景:如果你的应用有前台Service在运行(比如音乐播放、定位服务),则可以直接在后台启动Activity,不受限制。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 09:05:28