Core components to assist with consuming Azure Notification Hubs push notifications for Firebase
$ dotnet add package MobileNomad.MAUI.PushNotifications.FCMA comprehensive .NET MAUI library for implementing cross-platform push notifications using Azure Notification Hubs. This solution supports multiple push notification services including APNS (Apple Push Notification Service), FCM (Firebase Cloud Messaging), and WNS (Windows Notification Service).
This library provides a unified, platform-specific implementation for push notifications in .NET MAUI applications, leveraging Azure Notification Hubs as the central messaging service. It abstracts the complexity of platform-specific push notification implementations while maintaining full control over notification handling.
INotificationRegistrationService - Core registration interfaceNotificationRegistrationServiceBase - Base implementation for platform-specific servicesPushMessages - Static event system for notification handlingRegisterDeviceMessage - Data structure for device registrationNotificationRegistrationService implementationsUserNotificationDelegate for handling notification eventsAppDelegateHelpers for easy integration into AppDelegateNotificationRegistrationService with Google Play Services validation and Android device ID managementFCMMessagingService for handling incoming push notifications with configurable notification channelsAdd references to the required projects in your MAUI application:
<PackageReference Include="MobileNomad.MAUI.PushNotifications.Core" Version="1.0.0" />
<PackageReference Include="MobileNomad.MAUI.PushNotifications.APNS" Version="1.0.1" />
<PackageReference Include="MobileNomad.MAUI.PushNotifications.FCM" Version="1.0.2" />
Configure the notification services in your MauiProgram.cs:
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.SetupNotificationsCore(azureConnectionString, hubName)
.SetupNotificationsFCM() // For Android
.SetupNotificationsAPNS(); // For iOS/Mac Catalyst
return builder.Build();
}
}
You'll need:
AppDelegate:using MobileNomad.MAUI.PushNotifications.APNS;
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
AppDelegateHelpers.NotificationFinishedLaunching(this);
return base.FinishedLaunching(application, launchOptions);
}
}
INotificationRegistrationService to call the registration method: [Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
public void RegisteredForRemoteNotificationsWithDeviceToken(UIApplication application, NSData deviceToken)
{
var message = AppDelegateHelpers.GetRegisterDeviceMessage(deviceToken);
_notificationRegistrationService.RegisterDevice(message);
}
Ensure your google-services.json file is included in the Android platform folder.
Add the following to the project file to load the google-services.json file:
<ItemGroup>
<GoogleServicesJson Include="Platforms\Android\google-services.json" />
</ItemGroup>
AndroidManifest.xml has the following permissions:<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
App.xaml.cs file, in the OnStart event, you can ask the user for notificaiton permissions.
The library does have a default implementation to request permissions.
You can use it, or provide your own implementation to request permissions.
Like with iOS and MacCatalyst, you will need to get a reference to the Android implementation of
INotificationRegistrationService to register the device.#if ANDROID
var permissionStatus = await PermissionChecker.CheckPermissions();
if (permissionStatus == PermissionStatus.Granted)
{
var message = new RegisterDeviceMessage();
_notificationRegistrationService.RegisterDevice(message);
}
#endif
FCMMessagingService.DefaultChannelId = "some id value";
FCMMessagingService.ChannelName = "some name value";
PushMessages static classThe included sample application demonstrates:
Run the sample to see the push notification system in action across different platforms.