DEV Community

Cover image for Creating push notifications for Android in .NET MAUI
Serhii Korol
Serhii Korol

Posted on

Creating push notifications for Android in .NET MAUI

Hello colleagues. In this article, I want to introduce you to Firebase notification implementation. It's a super easy realization and I show how.

Creating Firebase project

Go to the and click on the huge + and begin creating a project.

1 step

Let's name the project "push-maui". Click "Continue".
In the second step leave it unchanged.

2 step

In the last step select an account and click "Create project".

3 step

After one minute - click "Continue".


Creating MAUI project

Let's create a new project:

dotnet new maui -n PushMauiSample
cd PushMauiSample
Enter fullscreen mode Exit fullscreen mode

Open the solution in your favorite IDE. Before we begin implementing Firebase Cloud Messaging, let's add Plugin.Firebase package. I install an older version since it is compatible with JDK. The newest version has issues:

dotnet add package Plugin.Firebase 1.2.0
Enter fullscreen mode Exit fullscreen mode

And also, for Android you need these packages:

dotnet add package Xamarin.Kotlin.StdLib.Jdk7
dotnet add package Xamarin.Kotlin.StdLib.Jdk8
Enter fullscreen mode Exit fullscreen mode

Further, go to the PushMauiSample.proj, delete all non-android settings, and inject Google services. It should be something like that:

<Project Sdk="Microsoft.NET.Sdk">



        <!-- Display name -->

        <!-- App Identifier -->

        <!-- Versions -->

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>

        <!-- App Icon -->
        <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

        <!-- Splash Screen -->
        <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

        <!-- Images -->
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />

        <!-- Custom Fonts -->
        <MauiFont Include="Resources\Fonts\*" />

        <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />

    <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-android'">
        <GoogleServicesJson Include="google-services.json" />

        <PackageReference Include="Plugin.Firebase" Version="1.2.0" />

    <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-android'">
      <PackageReference Include="Xamarin.Kotlin.StdLib.Jdk7" Version="1.8.22" />
      <PackageReference Include="Xamarin.Kotlin.StdLib.Jdk8" Version="1.8.22" />

      <MauiPlatformSpecificFolder Remove="Platforms\iOS\" />
      <MauiPlatformSpecificFolder Remove="Platforms\MacCatalyst\" />
      <MauiPlatformSpecificFolder Remove="Platforms\Tizen\" />
      <MauiPlatformSpecificFolder Remove="Platforms\Windows\" />


Enter fullscreen mode Exit fullscreen mode

After that, you did it. Let's register the service. Add this code to MauiProgram.cs:

private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
        builder.ConfigureLifecycleEvents(events =>

            events.AddAndroid(android => android.OnCreate((activity, state) =>
                CrossFirebase.Initialize(activity, CreateCrossFirebaseSettings())));

        builder.Services.AddSingleton(_ => CrossFirebaseAuth.Current);
        return builder;

    private static CrossFirebaseSettings CreateCrossFirebaseSettings()
        return new CrossFirebaseSettings(isAuthEnabled: true, isCloudMessagingEnabled: true);
Enter fullscreen mode Exit fullscreen mode

And sure, you should update this method:

public static MauiApp CreateMauiApp()
        var builder = MauiApp.CreateBuilder();
            .ConfigureFonts(fonts =>
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");

        return builder.Build();
Enter fullscreen mode Exit fullscreen mode

Registering an application

And now, let's return to Firebase Console and register the application. You should choose Android.

select app

There are four steps. The more important is first. The Android package name is project id. Let's name it Click the Register app.
In the next step, save google-services.json to the root of your project. Click Next.

save servvices

The next steps without changes. In the end, click "Continue to console".

A few settings and implementation

Let's return to PushMauiSample.proj and find this row:

Enter fullscreen mode Exit fullscreen mode

You should replace to name that we registered in the Firebase console. The same name you can find in google-services.json file. This name should identical.
Next, go to the MainPage.xaml and paste this code in VerticalStackLayout block:

                SemanticProperties.Description="Cute dot net bot waving hi to you!"
                Source="dotnet_bot.png" />

                Text="Hello, Firebase notifications" />

                SemanticProperties.Description="Welcome to dot net Multi platform App U I"
                Text="Welcome to .NET Multi-platform App UI" />

                SemanticProperties.Hint="Get registration id token"
                Text="Get Token"
                x:Name="CounterBtn" />
Enter fullscreen mode Exit fullscreen mode

After that, you should create an event handler like this:

private async void GetTokenClicked(object sender, EventArgs e)
        await CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
        var token = await CrossFirebaseCloudMessaging.Current.GetTokenAsync();
        Token = token;
        await DisplayAlert("FCM token", token, "OK");
Enter fullscreen mode Exit fullscreen mode

And don't forget to add a property for keeping token:

public string Token { get; set; }
Enter fullscreen mode Exit fullscreen mode

The first part is ready. Let's check out it.

main page

got token

We got a token that was registered in Firebase. Without this token, you can't send notifications.

Implementing sending notifications

And now, we need to configure sending notifications.
Let's return to the Firebase console again and find gears. Select the "Project settings" and "Service accounts" tab. Click "Generate new private key."


Save it to your project into Resources/Raw folder with name firebase-adminsdk.json.

After that return to MainPage.xaml and add the button:

                SemanticProperties.Hint="Send notification"
                Text="Send push"
                x:Name="SendPush" />
Enter fullscreen mode Exit fullscreen mode

Add handler to MainPage.cs. Here it loads the file sdk settings and creates an application that sends a notification:

private async void SendPushClicked(object sender, EventArgs e)
        var app = FirebaseApp.Create(new AppOptions
            Credential = await GetCredential()

        FirebaseMessaging messaging = FirebaseMessaging.GetMessaging(app);
        var message = new Message()
            Token = Token,
            Notification = new Notification { Title = "Hello world!", Body = "It's a message for Android with MAUI"},
            Data = new Dictionary<string, string> {{"greating", "hello"}},
            Android = new AndroidConfig { Priority = Priority.Normal},
            Apns = new ApnsConfig { Headers = new Dictionary<string, string> { {"apns-priority", "5"}}}
        var response = await messaging.SendAsync(message);
        await DisplayAlert("Response", response, "OK");

    private async Task<GoogleCredential> GetCredential()
        var path = await FileSystem.OpenAppPackageFileAsync("firebase-adminsdk.json");
        return GoogleCredential.FromStream(path);
Enter fullscreen mode Exit fullscreen mode

You'll see a lot of unresolved code, it means you need the FirbaseAdminCore package:

dotnet add package FirbaseAdminCore
Enter fullscreen mode Exit fullscreen mode

After installing your code will resolved. And naw became time check out our application.


Getting token:


Sending push:


Finally, we got the notification:



It's an easy way to make notifications for mobile devices. Put likes on and subscribe. Happy coding!

Source code here.

Buy Me A Beer

Top comments (5)

vlinakis profile image
Vasilis Linakis

Hi Serhii,

Excellent post and work! Thank you very much.
I get everything working right but the Push notification doesn't arrive. I get a warning about the Channel being Null.

Developer warning for package "". Failed to post notification on channel "null". See log for more details.

Is there anyway you can help?

Also, have you created an iOS version of your solution?

Thanks again!

serhii_korol_ab7776c50dba profile image
Serhii Korol

I hadn't created it for iOS since I don't have an Apple Developer account. Likely, iOS requires additional settings. It needs investigating. I recalled why I didn't make it for iOS. I was having issues with packages. For this reason, I refused the idea. The iOS is capricious.

vlinakis profile image
Vasilis Linakis

Thanks for your immediate reply.
How about the issue with the channel? Have you managed to reproduce it and maybe solve it?

clauselmann profile image

Hi Serhii

Just pulled you repo and replaced google-services.json and firebase-adminsdk.json
and ran dotnet restore

But I then get this, while trying the get token:

Java.Lang.IllegalStateException: 'Default FirebaseApp is not initialized in this process Make sure to call FirebaseApp.initializeApp(Context) first.'

Any help is appreciated

serhii_korol_ab7776c50dba profile image
Serhii Korol

Check all settings it should work fine. I just checked this out and I didn't find any issues. Or describe your issue in more detail. Make steps to reproduce.