Can't change background color of Android notification using Firebase and dotnet Maui
I am using Firebase to send push notifications to a .NET8 MAUI app... The notifications work, but the background color of the notification shows as a very light grey, making it impossible to see the icon (pictures included below)... There is supposedly a workaround to add a line to the AndroidManifest.xml file, but that doesn't appear to fix the problem..... Here is the relevant part of my code: AndroidManifest.xml Platforms\Android\Resources\values\colors.xml #FF0000 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() .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 net8.0-ios;net8.0-android Exe true True \ I also obviously have my google-services.json file in there as well and the app created in the Firebase console.... When I send a test message from the console, the notification comes through. BUT, you will see when I swipe down, the icon background is nearly white. Here is my icon file Here is the notification prior to swiping down. Looks beautiful!!! Here is my notification when I swipe down, but you can barely see the icon because the background color is so light: Any ideas at all?????
I am using Firebase to send push notifications to a .NET8 MAUI app... The notifications work, but the background color of the notification shows as a very light grey, making it impossible to see the icon (pictures included below)...
There is supposedly a workaround to add a line to the AndroidManifest.xml file, but that doesn't appear to fix the problem.....
Here is the relevant part of my code:
AndroidManifest.xml
Platforms\Android\Resources\values\colors.xml
#FF0000
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()
.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
net8.0-ios;net8.0-android
Exe
true
True
\
I also obviously have my google-services.json file in there as well and the app created in the Firebase console....
When I send a test message from the console, the notification comes through. BUT, you will see when I swipe down, the icon background is nearly white.
Here is the notification prior to swiping down. Looks beautiful!!!
Here is my notification when I swipe down, but you can barely see the icon because the background color is so light:
Any ideas at all?????