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

Flutter Firebase推送通知图标在安卓深色模式下颜色不一致求助

解决Android深色模式下Firebase通知图标颜色变浅的问题

针对你使用Flutter 3.10.4 + Firebase Messaging在Android 13上遇到的通知图标颜色在深色模式下变浅的问题,以下是具体解决方案:

问题根源

Android 13(API 33)的通知系统会根据系统明暗主题自动调整通知元素的色调,即使配置了default_notification_color,深色模式下系统仍可能对颜色做亮度/透明度适配,导致颜色变浅。Firebase Messaging本身不直接控制系统深色模式,但可以通过Android原生配置强制固定通知图标颜色。

解决方案

1. 确保深色模式下颜色值一致

检查android/app/src/main/res/values-night/color.xml文件,确保其中的colorPrimary与浅色模式保持一致:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#5E3DEA</color>
</resources>

如果该文件夹不存在,直接创建即可。

2. 创建自定义通知通道并强制固定颜色

Android 8.0+要求使用通知通道,创建通道时设置固定颜色,并将其设为Firebase的默认通道,确保后台推送的通知也遵循该配置:

步骤1:在Flutter中创建通知通道

使用flutter_local_notifications插件(需先添加依赖到pubspec.yaml),初始化时创建通道:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

final FlutterLocalNotificationsPlugin _notificationsPlugin = FlutterLocalNotificationsPlugin();

Future<void> _initializeNotifications() async {
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('@drawable/robot');
  
  const InitializationSettings initializationSettings =
      InitializationSettings(android: initializationSettingsAndroid);
  
  await _notificationsPlugin.initialize(initializationSettings);

  // 创建自定义通知通道
  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'push_notification_channel', // 通道ID
    '推送通知', // 通道名称
    description: '接收Firebase推送通知的通道', // 通道描述
    importance: Importance.max,
    color: Color(0xFF5E3DEA), // 强制设置通知图标颜色
    enableLights: true,
    lightColor: Color(0xFF5E3DEA),
  );

  await _notificationsPlugin
      .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);
}

在main()函数中调用_initializeNotifications()完成初始化。

步骤2:指定Firebase默认通道ID

修改AndroidManifest.xml,添加默认通道配置:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="push_notification_channel" />

确保通道ID与Flutter代码中创建的一致。

3. 自定义前台通知接收逻辑(可选)

如果需要完全控制通知显示,拦截Firebase的前台消息并手动构建通知:

import 'package:firebase_messaging/firebase_messaging.dart';

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  RemoteNotification? notification = message.notification;
  AndroidNotification? android = message.notification?.android;

  if (notification != null && android != null) {
    _notificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      NotificationDetails(
        android: AndroidNotificationDetails(
          'push_notification_channel',
          '推送通知',
          channelDescription: '接收Firebase推送通知的通道',
          icon: '@drawable/robot',
          color: const Color(0xFF5E3DEA), // 强制固定颜色
        ),
      ),
    );
  }
});

4. 检查通知图标配置

如果使用矢量图标(drawable/robot.xml),确保图标未使用主题属性着色:

  • 打开图标文件,移除类似android:tint="?attr/colorOnSurface"的配置
  • 若需要固定颜色,直接设置android:tint="#5E3DEA"

验证

完成配置后,重新打包安装应用,切换系统深色模式,测试Firebase推送通知,图标颜色应与浅色模式保持一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 08:27:03