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
- Go to the Firebase Console.
- Click “Add Project”, enter a name, and follow the instructions.
- Enable Google Analytics if needed.
Step 2: Add Your iOS App
- Register your iOS app’s bundle identifier in Firebase.
- Download the
GoogleService-Info.plist
file. - 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
- Install the required NuGet packages for Firebase Cloud Messaging.
- 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
- Go to Firebase Console → Cloud Messaging → Send a Message.
- Enter the title, body, and FCM token.
- Select iOS and send the message.
- 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!
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.