使用Firebase与.NET MAUI无法修改Android通知背景颜色
.NET 8 MAUI Firebase推送通知图标背景异常问题
我在.NET 8 MAUI应用中集成Firebase推送通知,通知可正常接收,但下拉通知栏后,通知图标背景为极浅灰色,导致图标几乎无法辨认。尝试通过AndroidManifest.xml添加配置项解决,但未生效,相关配置代码如下:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.mycompany.myapp"> <application android:icon="@mipmap/appicon"> <!-- sets the icon, which it is doing correctly. --> <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/push_notification_icon" /> <!-- This should set the background color of the icon when you swipe down, but it doesnt seem to work --> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/default_notification_color" tools:replace="android:resource" /> <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" /> <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="${applicationId}" /> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> </manifest>
Platforms\Android\Resources\values\colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="default_notification_color">#FF0000</color> </resources>
MauiProgram.cs
using Plugin.Firebase.Auth; using Plugin.Firebase.Bundled.Shared; #if IOS using Plugin.Firebase.Bundled.Platforms.iOS; #elif ANDROID using Plugin.Firebase.Bundled.Platforms.Android; #endif public static class MauiProgram { public static MauiApp CreateMauiApp() { return MauiApp .CreateBuilder() .UseMauiApp<App>() .UseMauiCompatibility() .UseMauiCommunityToolkit() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }) .RegisterFirebaseServices() .Build(); } private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder) { builder.ConfigureLifecycleEvents(events => { #if IOS events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => { CrossFirebase.Initialize(CreateCrossFirebaseSettings()); return false; })); #elif ANDROID events.AddAndroid(android => android.OnCreate((activity, _) => CrossFirebase.Initialize(activity, CreateCrossFirebaseSettings()) )); #endif }); builder.Services.AddSingleton(_ => CrossFirebaseAuth.Current); return builder; } private static CrossFirebaseSettings CreateCrossFirebaseSettings() { return new CrossFirebaseSettings( isAuthEnabled: true, isCloudMessagingEnabled: true, isAnalyticsEnabled: false); } }
Platforms\Android\MainActivity.cs
using Plugin.Firebase.CloudMessaging; [Activity(Label = "MyApp", Theme = "@style/Maui.SplashTheme", MainLauncher = true)] public class MainActivity : MauiAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { if (OperatingSystem.IsAndroidVersionAtLeast(33)) { if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.PostNotifications) != Permission.Granted) { ActivityCompat.RequestPermissions(this, new[] { Manifest.Permission.PostNotifications }, 0); } } HandleIntent(Intent); CreateNotificationChannelIfNeeded(); } protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); HandleIntent(intent); } private static void HandleIntent(Intent intent) { FirebaseCloudMessagingImplementation.OnNewIntent(intent); } private void CreateNotificationChannelIfNeeded() { if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { CreateNotificationChannel(); } } private void CreateNotificationChannel() { if (OperatingSystem.IsAndroidVersionAtLeast(26)) { var channelId = $"{PackageName}.general"; var notificationManager = (NotificationManager)GetSystemService(NotificationService); var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default); notificationManager.CreateNotificationChannel(channel); FirebaseCloudMessagingImplementation.ChannelId = channelId; FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.push_notification_icon; } } }
MyApp.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net8.0-ios;net8.0-android</TargetFrameworks> <OutputType>Exe</OutputType> <UseMaui>true</UseMaui> <!-- lots more here removed --> </PropertyGroup> <ItemGroup> <!-- App Icon --> <MauiIcon Include="Resources\AppIcon\appicon.png" BaseSize="48,48" Color="#01CAFF" /> <!-- Images --> <MauiImage Include="Resources\Images\*" /> </ItemGroup> <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'"> <GoogleServicesJson Include="Platforms\Android\google-services.json" /> <PackageReference Include="Xamarin.AndroidX.Core" Version="1.12.0.3" /> <PackageReference Include="Xamarin.AndroidX.Collection" Version="1.3.0.2" /> <PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.3.0.2" /> <PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.8.1.1" /> <PackageReference Include="Xamarin.Google.Android.Material" Version="1.10.0.2" /> </ItemGroup> <ItemGroup> <MauiImage Update="Resources\Images\push_notification_icon.png" Resize="false" /> </ItemGroup> <ItemGroup> <PackageReference Include="CommunityToolkit.Maui" Version="9.0.3" /> <PackageReference Include="ExifLib.Standard" Version="1.7.0" /> <PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.8" /> <PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.8.0" /> <PackageReference Include="Plugin.Firebase" Version="3.0.0" /> <PackageReference Include="Xamarin.Build.Download" Version="0.11.4" /> </ItemGroup> </ItemGroup> <ItemGroup> <None Update="Resources\Raw\app_store_logo128x128.png"> <Pack>True</Pack> <PackagePath>\</PackagePath> </None> </ItemGroup> </Project>
我已在Firebase控制台创建应用并配置google-services.json,测试通知可正常接收,但图标背景问题未解决。
解决思路
1. 符合Android通知图标规范
Android通知图标要求为透明背景的单色(白色)资源,若push_notification_icon.png不符合:
- 确保图标背景完全透明
- 图标主体为白色(系统会根据通知栏主题自动适配颜色)
- 提供多尺寸适配(如24x24dp、48x48dp等),避免缩放导致的显示问题
2. 为NotificationChannel设置颜色
Android 8.0+的通知栏会使用Channel的颜色作为图标背景,修改CreateNotificationChannel方法,添加颜色配置:
private void CreateNotificationChannel() { if (OperatingSystem.IsAndroidVersionAtLeast(26)) { var channelId = $"{PackageName}.general"; var notificationManager = (NotificationManager)GetSystemService(NotificationService); var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default); // 关联自定义颜色资源 channel.SetColor(ContextCompat.GetColor(this, Resource.Color.default_notification_color)); notificationManager.CreateNotificationChannel(channel); FirebaseCloudMessagingImplementation.ChannelId = channelId; FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.push_notification_icon; } }
3. 验证颜色资源的有效性
- 确认
Platforms\Android\Resources\values\colors.xml的Build Action为AndroidResource - 清理项目缓存后重新构建,避免资源未正确打包
4. 处理数据消息的自定义通知
若发送的是Firebase数据消息(而非通知消息),需手动构建通知并设置颜色:
// 示例:处理数据消息时构建通知 var notificationBuilder = new NotificationCompat.Builder(this, channelId) .SetSmallIcon(Resource.Drawable.push_notification_icon) .SetContentTitle(title) .SetContentText(message) .SetColor(ContextCompat.GetColor(this, Resource.Color.default_notification_color)) .SetAutoCancel(true); NotificationManagerCompat.From(this).Notify(0, notificationBuilder.Build());
5. 适配不同Android版本
- Android 7.1及以下:
AndroidManifest.xml中的default_notification_color配置生效 - Android 8.0+:必须通过NotificationChannel设置颜色,Manifest配置会被Channel覆盖
内容的提问来源于stack exchange,提问作者Mike Luken
相关产品推荐
相关产品推荐

