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

Android应用Firebase FCM通知不显示问题求助及代码排查

FCM通知无法显示问题排查与修复

问题背景

Android应用已成功接入Firebase,前台状态下onMessageReceived方法正常触发,日志可捕获通知内容,但FCM通知始终无法显示。已完成以下配置:

  • Manifest中添加了默认通知渠道、图标、颜色的meta-data
  • 在MainActivity的onCreate和自定义FirebaseMessagingService中调用了通知渠道创建方法

代码问题分析

查看提供的MyFirebaseMessagingService和Manifest代码,核心问题集中在通知渠道ID不匹配及资源引用错误:

1. 通知渠道ID完全不匹配

  • 创建渠道时使用的ID是"default_notification_channel_id"(或对应字符串资源),但构建通知时硬编码为"channel_id",导致通知找不到对应渠道,无法显示。
  • 类中定义的CHANNEL_ID使用String.valueOf(R.string.default_notification_channel_id),这会将资源ID的数值转为字符串,而非获取字符串的实际内容,属于资源引用错误。

2. 通知优先级与渠道优先级不一致

渠道设置为IMPORTANCE_HIGH,但通知Builder中使用PRIORITY_DEFAULT,虽然后者会被渠道优先级覆盖,但保持一致性可避免潜在问题。

3. 渠道创建时机滞后

当前在收到消息时才创建渠道,若首次收到消息时渠道未完成注册,会导致通知无法显示。

修复方案

步骤1:统一通知渠道ID

确保所有位置使用的渠道ID完全一致,且正确获取字符串资源:

修改MyFirebaseMessagingService中的渠道ID引用

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public static final String TAG = "Push-Event";

    @Override
    public void onNewToken(@NonNull String token) {
        Log.d(TAG, "Refreshed token: " + token);
    }
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        if (remoteMessage.getNotification() != null) {
            String title = remoteMessage.getNotification().getTitle();
            String description = remoteMessage.getNotification().getBody();
            Log.d(TAG, "Message Notification Title: " + title);
            Log.d(TAG, "Message Notification Description: " + description);
            
            createNotificationChannel();
            // 使用正确的渠道ID字符串
            String channelId = getString(R.string.default_notification_channel_id);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                    .setContentTitle(title)
                    .setContentText(description)
                    .setPriority(NotificationCompat.PRIORITY_HIGH) // 与渠道优先级匹配
                    .setStyle(new NotificationCompat.BigTextStyle())
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(0, notificationBuilder.build());
        }
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = getString(R.string.default_notification_channel_id);
            NotificationChannel channel = new NotificationChannel(
                    channelId,
                    "Channel name",
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("Channel description");

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }
}

步骤2:确保字符串资源存在

检查res/values/strings.xml中是否定义了对应字符串:

<string name="default_notification_channel_id">default_notification_channel</string>

步骤3:提前创建渠道

在MainActivity的onCreate中调用createNotificationChannel,确保应用启动时渠道已注册:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    createNotificationChannel();
    // 其他初始化代码
}

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = getString(R.string.default_notification_channel_id);
        NotificationChannel channel = new NotificationChannel(
                channelId,
                "Channel name",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription("Channel description");

        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
}

步骤4:Android 13+动态申请通知权限

Manifest中已添加POST_NOTIFICATIONS权限,需在代码中动态申请:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
        ActivityResultContracts.RequestPermission requestPermission = new ActivityResultContracts.RequestPermission();
        registerForActivityResult(requestPermission, isGranted -> {
            // 可根据权限状态做后续处理
        }).launch(Manifest.permission.POST_NOTIFICATIONS);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 10:20:53