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

Flutter中如何修改Firebase通知图标背景色?FCM通知图标白背景问题

解决FCM通知图标白色圆圈背景问题

首先明确:Android 5.0(API 21)及以上系统对通知图标有强制规范,要求图标必须是白色前景+透明背景的矢量资源,系统会自动为图标添加背景色。你遇到的第一个通知白圈问题,大概率是以下几个原因导致的,对应解决方案如下:

1. 确保FCM默认通知的Manifest配置正确

你之前添加的default_notification_color未生效,可能是缺少了默认图标配置,或者代码格式有误。请在AndroidManifest.xml的<application>标签内补充完整配置:

<!-- 指定FCM默认通知图标(必须是符合规范的白图标) -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_notification_white" />

<!-- 设置通知图标背景色为黑色 -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/black" />

注意:@drawable/ic_notification_white必须是仅含白色前景、透明背景的矢量图,不能带黑色背景,否则系统仍会强制添加白圈。

2. 自定义通知需显式设置颜色

如果你的第二个通知是通过FirebaseMessagingService自定义构建的,那需要在NotificationCompat.Builder中直接指定颜色:

// Kotlin示例
val notificationBuilder = NotificationCompat.Builder(this, YOUR_CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_notification_white)
    .setColor(ContextCompat.getColor(this, R.color.black)) // 强制设置图标背景色
    .setContentTitle(notificationTitle)
    .setContentText(notificationContent)
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Java示例
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, YOUR_CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notification_white)
        .setColor(ContextCompat.getColor(this, R.color.black))
        .setContentTitle(notificationTitle)
        .setContentText(notificationContent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

3. Android 8.0+需同步配置通知渠道颜色

Android 8.0(API 26)及以上的通知渠道颜色会覆盖全局配置,所以创建渠道时要设置对应颜色:

// Kotlin示例
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(
        YOUR_CHANNEL_ID,
        "通知渠道名称",
        NotificationManager.IMPORTANCE_DEFAULT
    ).apply {
        description = "通知渠道描述"
        // 设置渠道关联的通知图标背景色
        lightColor = ContextCompat.getColor(this@MyFirebaseMessagingService, R.color.black)
        enableLights(true)
    }
    val notificationManager = getSystemService(NotificationManager::class.java)
    notificationManager.createNotificationChannel(channel)
}

4. 清除应用缓存测试

系统可能会缓存旧的通知图标配置,建议卸载重装应用,或者清除应用的缓存数据后再发送通知测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 05:22:48