SkillAgentSearch skills...

Plugin.FirebasePushNotifications

Receive and handle firebase push notifications in .NET MAUI apps

Install / Use

/learn @thomasgalliker/Plugin.FirebasePushNotifications
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Plugin.FirebasePushNotifications

Version Downloads Buy Me a Coffee

Plugin.FirebasePushNotifications provides a seamless way to engage users and keep them informed about important events in your .NET MAUI applications. This open-source C# library integrates Firebase Cloud Messaging (FCM) into your .NET MAUI projects, enabling you to receive push notifications effortlessly.

Features

  • Cross-platform Compatibility: Works seamlessly with .NET MAUI, ensuring a consistent push notification experience across different devices and platforms.
  • Easy Integration: Simple setup process to incorporate Firebase Push Notifications into your .NET MAUI apps.
  • Flexible Messaging: Utilize FCM's powerful features, such as targeted messaging, to send notifications based on user segments or specific conditions.

Download and Install Plugin.FirebasePushNotifications

This library is available on NuGet: https://www.nuget.org/packages/Plugin.FirebasePushNotifications Use the following command to install Plugin.FirebasePushNotifications using NuGet package manager console:

PM> Install-Package Plugin.FirebasePushNotifications

You can use this library in any .NET MAUI project compatible to .NET 7 and higher.

Setup

Setup Firebase Push Notifications

  • Go to https://console.firebase.google.com and create a new project. The setup of Firebase projects is not (yet?) documented here. Contributors welcome!
  • You have to download the resulting Firebase service files and integrate them into your .NET MAUI csproj file. google-services.json is used by Android while GoogleService-Info.plist is accessible to iOS. Make sure the Include and the Link paths match.
<ItemGroup Condition="$(TargetFramework.Contains('-android'))">
	<GoogleServicesJson Include="Platforms\Android\Resources\google-services.json" Link="Platforms\Android\Resources\google-services.json" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.Contains('-ios'))">
	<BundleResource Include="Platforms\iOS\GoogleService-Info.plist" Link="GoogleService-Info.plist" />
</ItemGroup>
  • iOS apps need to be enabled to support push notifications. Turn on the "Push Notifications" capability of your app in the Apple Developer Portal.

MAUI App Startup

This plugin provides an extension method for MauiAppBuilder UseFirebasePushNotifications which ensure proper startup and initialization. Call this method within your MauiProgram just as demonstrated in the MauiSampleApp:

var builder = MauiApp.CreateBuilder()
    .UseMauiApp<App>()
    .UseFirebasePushNotifications();

UseFirebasePushNotifications has optional configuration parameters which are documented in another section of this document.

Android-specific Setup

  • Copy the google-services.json to path location Platforms\Android\Resources\google-services.json (depending on what is configured in the csproj file).
  • Make sure your launcher activity (usually this is MainActivity - but not always) uses LaunchMode = LaunchMode.SingleTask. You can also use a different LaunchMode; just be very sure what you do!

iOS-specific Setup

  • Copy the GoogleService-Info.plist to path location Platforms\iOS\GoogleService-Info.plist (depending on what is configured in the csproj file).
  • Extend the AppDelegate.cs file with following method exports:
[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
[BindingImpl(BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
public void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    IFirebasePushNotification.Current.RegisteredForRemoteNotifications(deviceToken);
}

[Export("application:didFailToRegisterForRemoteNotificationsWithError:")]
[BindingImpl(BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
public void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
    IFirebasePushNotification.Current.FailedToRegisterForRemoteNotifications(error);
}

[Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]
public void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    IFirebasePushNotification.Current.DidReceiveRemoteNotification(userInfo);
    completionHandler(UIBackgroundFetchResult.NewData);
}

API Usage

IFirebasePushNotification is the main interface which handles most of the desired Firebase push notification features. This interface is injectable via dependency injection or accessible as a static singleton instance IFirebasePushNotification.Current. We strongly encourage you to use the dependency injection approach in order to keep your code testable.

The following lines of code demonstrate how the IFirebasePushNotification instance is injected in MainViewModel and assigned to a local field for later use:

public MainViewModel(
    ILogger<MainViewModel> logger,
    IFirebasePushNotification firebasePushNotification)
{
    this.logger = logger;
    this.firebasePushNotification = firebasePushNotification;
}

Notification Permissions

Before we can receive any notification we need to make sure the user has given consent to receive notifications. INotificationPermissions is the service you can use to check the current authorization status or ask for notification permission. You can either inject INotificationPermissions into your view models or access it via the the static singleton instance INotificationPermissions.Current.

  • Check the current notification permission status:
this.AuthorizationStatus = await this.notificationPermissions.GetAuthorizationStatusAsync();
  • Ask the user for notification permission:
await this.notificationPermissions.RequestPermissionAsync();

Notification permissions are handled by the underlying operating system (iOS, Android). This library just wraps the platform-specific methods and provides a uniform API for them.

Register for Notifications

The main goal of a push notification client library is to receive notification messages. This library provides a set of classic .NET events to inform your code about incoming push notifications. Before any notification event is received, we have to inform the Firebase client library, that we're ready to receive notifications. RegisterForPushNotificationsAsync registers our app with the Firebase push notification backend and receives a token. This token is used by your own server/backend to send push notifications directly to this particular app instance. The token may change after some time. It is not controllable by this library if/when the token is going to be updated. The TokenRefreshed event will be fired whenever a new token is available. See Token property and TokenRefreshed event provided by IFirebasePushNotification for more info.

await this.firebasePushNotification.RegisterForPushNotificationsAsync();

If we want to turn off any incoming notifications, we can unregister from push notifications. The Token can no longer be used to send push notifications to.

await this.firebasePushNotification.UnregisterForPushNotificationsAsync();

Receive Notifications

Following .NET events can be subscribed.

| Events | Description | |------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | TokenRefreshed | Is raised whenever the Firebase push notification token is updated. You'll need to inform your server/backend whenever a new push notification token is available. | | NotificationReceived | Is raised when a new push notification message was received. | | NotificationOpened | Is raised when a received push notification is opened. This means, a user taps on a received notification listed in the notification center provided by the OS. | | NotificationAction | Is raised when the user taps a notification action. Notification actions allow users to make simple decisions when a notification is received, e.g. "Do you like to take your medicine?" could be answered with "Take medicine" and "Skip medicine". | | NotificationDeleted | Is raised when the user deletes a received notification. |

Notification Handling Behavior

The following table documents the behavior of each platform for incoming push notifications. We distinguish between notification message and data message.

  • Notification messages must have a notification part with title and/or body. Other sections such as `dat

Related Skills

View on GitHub
GitHub Stars116
CategoryDevelopment
Updated24d ago
Forks12

Languages

C#

Security Score

100/100

Audited on Mar 12, 2026

No findings