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

Android Java集成FCM:后台/杀死状态下自定义通知不显示求助

解决FCM后台/杀死状态下自定义通知不显示的问题

一、确保发送纯数据消息

FCM在应用后台或被杀死状态下,只有纯数据消息会触发FirebaseMessagingService的onMessageReceived方法。如果消息包含notification字段,系统会直接展示默认通知,完全跳过你的自定义逻辑。

  • 正确的服务器端消息格式:
{
  "to": "目标设备TOKEN",
  "data": {
    "title": "自定义通知标题",
    "content": "自定义通知内容",
    "custom_data": "你的业务数据"
  }
}
  • 禁止使用带notification字段的消息格式,否则后台状态下系统会直接接管通知展示。

二、正确实现FirebaseMessagingService

1. 自定义消息处理Service

创建继承自FirebaseMessagingService的类,在onMessageReceived中处理纯数据消息并构建自定义通知:

public class CustomFcmService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // 仅处理纯数据消息
        if (!remoteMessage.getData().isEmpty()) {
            String title = remoteMessage.getData().get("title");
            String content = remoteMessage.getData().get("content");
            String customData = remoteMessage.getData().get("custom_data");

            // 调用自定义通知构建方法
            sendCustomNotification(title, content, customData);
        }
    }

    private void sendCustomNotification(String title, String content, String customData) {
        // Android 8.0+ 必须创建通知渠道
        String channelId = "fcm_custom_channel";
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    channelId,
                    "FCM自定义通知",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            notificationManager.createNotificationChannel(channel);
        }

        // 构建点击通知后的跳转Intent
        Intent intent = new Intent(this, TargetActivity.class);
        intent.putExtra("custom_data", customData);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        // 构建通知内容
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_notification_icon)
                .setContentTitle(title)
                .setContentText(content)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);

        // 显示通知
        notificationManager.notify((int) System.currentTimeMillis(), builder.build());
    }
}

2. 注册Service到Manifest

必须在AndroidManifest.xml中声明该Service,否则系统无法触发消息回调:

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

三、处理厂商后台限制

国内主流Android厂商(小米、华为、OPPO、VIVO等)存在后台进程管控机制,应用被杀死后可能无法触发onMessageReceived。解决措施:

  • 引导用户将应用加入后台白名单(自启动允许、电池优化忽略)
  • 针对部分厂商,可通过代码跳转至对应的系统设置页面完成配置
  • 避免设置过高的通知优先级,防止被系统判定为垃圾通知拦截

四、验证与调试

  • 使用FCM控制台发送纯数据消息(不要勾选「通知」选项),测试后台/杀死状态下是否触发onMessageReceived
  • 查看Logcat,过滤FirebaseMessaging标签,确认消息是否被正常接收
  • 检查google-services.json是否正确放置在app目录,FCM相关依赖是否完整引入

替代方案

若上述方法仍受厂商限制影响,可考虑:

  • 给纯数据消息添加priority: "high"(服务器端配置),提升消息送达优先级,但注意不要滥用,避免被系统限流
  • 集成国内第三方推送服务(如极光推送、个推),这类服务针对国内厂商做了适配优化,能有效提升后台消息的送达率

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 12:15:08