Max Mannstein

Subscribe :)

Implementing Firebase Cloud Messaging in .NET MAUI (iOS)

Push notifications are a crucial feature for keeping users engaged. In this guide, you’ll learn how to set up Firebase Cloud Messaging (FCM) in a .NET MAUI iOS app and test push notifications.


Why Use Firebase Cloud Messaging?

Firebase Cloud Messaging (FCM) allows you to send reliable push notifications to iOS and Android devices. This guide covers everything you need to set up FCM for iOS in your .NET MAUI project.


Step 1: Create a Firebase Project

  1. Go to the Firebase Console.
  2. Click “Add Project”, enter a name, and follow the instructions.
  3. Enable Google Analytics if needed.

Step 2: Add Your iOS App

    1. Register your iOS app’s bundle identifier in Firebase.
    2. Download the GoogleService-Info.plist file.
    3. Place the file in your .NET MAUI solution folder and include it in your project:

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-ios'">
  <BundleResource Include="GoogleService-Info.plist" />
</ItemGroup>

Step 3: Configure iOS App Store Settings

  • Set up the necessary identifiers, keys and provisioning profiles in the Apple Developer Console.
  • Upload them to Firebase to enable push notifications.
  • Do everything as shown in the video

Step 4: Modify iOS Configurations

Create an Entitlements.plist file and add the following entry:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>aps-environment</key>
		<string>production</string>
	</dict>
</plist>

Step 5: Install Firebase Packages

  1. Install the required NuGet packages for Firebase Cloud Messaging.
  2. Add Firebase initialization to MauiProgram.cs:

using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;
using Plugin.Firebase.CloudMessaging;

#if IOS
using Plugin.Firebase.Core.Platforms.iOS;
#elif ANDROID
using Plugin.Firebase.Core.Platforms.Android;
#endif

namespace FirebaseCloudMessagingTutorial
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .RegisterFirebaseServices()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });

#if DEBUG
    		builder.Logging.AddDebug();
#endif

            return builder.Build();
        }
        private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
        {
            builder.ConfigureLifecycleEvents(events => {
#if IOS
        events.AddiOS(iOS => iOS.WillFinishLaunching((_, __) => {
            CrossFirebase.Initialize();
            FirebaseCloudMessagingImplementation.Initialize();
            return false;
        }));
#elif ANDROID
        events.AddAndroid(android => android.OnCreate((activity, _) =>
        CrossFirebase.Initialize(activity)));
#endif
            });

            return builder;
        }

    }
}

Testing Push Notifications on iOS

Step 1: Retrieve the Firebase Device Token

Add this code in MainPage.xaml.cs to fetch the device token:

    private async void OnCounterClicked(object sender, EventArgs e)
    {
        await CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
        var token = await CrossFirebaseCloudMessaging.Current.GetTokenAsync();
        Console.WriteLine($"FCM token: {token}");
    }

Log this token and use it to send test notifications.


Step 2: Send a Test Notification

  1. Go to Firebase ConsoleCloud MessagingSend a Message.
  2. Enter the title, body, and FCM token.
  3. Select iOS and send the message.
  4. Check if the notification appears on your device.

Wrapping Up

You’ve successfully implemented Firebase Cloud Messaging in your .NET MAUI iOS app! 🚀

Next Steps:

  • Ensure your certificates and provisioning profiles are valid.
  • Handle notifications when the app is in the background or killed.
  • Move to the Android setup (covered in the next post).

👉 If this guide helped, share it and subscribe for more .NET MAUI tutorials!

1 comment on “Implementing Firebase Cloud Messaging in .NET MAUI (iOS)

  1. Hello, how are you? Thank you very much for this wonderful step by step.

    I have a little problem and it is regarding the token generated in iOS. It gets stuck and I can’t figure out how to remove it, revoke it, change the token again. I have already uninstalled the app, removed the Apple Cloud backup, restarted the device and still, it does not work. Is there any method to be able to delete that FCM token and generate it again?

    It has worked for me only in the lifecycle of that token, but it seems that using the REST API of https://fcm.googleapis.com/v1/projects/{projectid}/messages:send
    it says it has expired and no notifications have come back on my device. I am using Visual Studio 2022 version 17.12.4 on a physical iPhone 11.

    I appreciate your kind reply.

Leave a Reply

Your email address will not be published. Required fields are marked *