Plugin.FirebasePushNotifications 2.2.26-pre

This is a prerelease version of Plugin.FirebasePushNotifications.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Plugin.FirebasePushNotifications --version 2.2.26-pre
NuGet\Install-Package Plugin.FirebasePushNotifications -Version 2.2.26-pre
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Plugin.FirebasePushNotifications" Version="2.2.26-pre" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Plugin.FirebasePushNotifications --version 2.2.26-pre
#r "nuget: Plugin.FirebasePushNotifications, 2.2.26-pre"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Plugin.FirebasePushNotifications as a Cake Addin
#addin nuget:?package=Plugin.FirebasePushNotifications&version=2.2.26-pre&prerelease

// Install Plugin.FirebasePushNotifications as a Cake Tool
#tool nuget:?package=Plugin.FirebasePushNotifications&version=2.2.26-pre&prerelease

Plugin.FirebasePushNotifications

Version Downloads

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.
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.

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 CrossFirebasePushNotification.Current. We highly 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;
}
Managing 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.

  • 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.

Receive Notifications

Now, 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 notfication backend and receives a Token. This Token is used by your own server/backend to send push notifications directly to this particular app instance. 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();

Following .NET events can be subscribed:

  • IFirebasePushNotification.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.

  • IFirebasePushNotification.NotificationReceived is raised when a new push notification message was received.

  • IFirebasePushNotification.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.

  • IFirebasePushNotification.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".

  • IFirebasePushNotification.NotificationDeleted is raised when the user deletes a received notification.

  • IFirebasePushNotification.NotificationError is raised in some particular error cases. _(Will be removed in future releases). _

Topics

The most common way of sending push notifications is by targeting notification message directly to push tokens. Firebase allows to send push notifications to groups of devices, so called topics. If a user subscribes to a topic, e.g. "weather_updates" you can send push notifications to this topic instead of a list of push tokens.

Subscribe to Topics

Use method SubscribeTopic with the name of the topic:

this.firebasePushNotification.SubscribeTopic("weather_updates");
Send Notifications to Topic Subscribers

Use the Firebase Admin SDK (or any other HTTP client) to send a push notification targeting subscribers of the "weather_updates" topic:

HTTP POST https://fcm.googleapis.com/fcm/send

{
    "data": {
        "title" : "Weather Update",
        "body": "Pleasant with clouds and sun"
     },
     "priority": "high",
     "condition": "'weather_updates' in topics"
}

Notification Topic weather_updates

Notification Actions

Notification actions are special buttons which allow for immediate response to a particular notification. A list of NotificationActions is consolidated within a NotificationCategory.

Register Notification Actions

The following example demonstrates the registration of a notification category with identifier "medication_intake" and two actions "Take medicine" and "Skip medicine":

var categories = new[]
{
    new NotificationCategory("medication_intake", new[]
    {
        new NotificationAction("take_medication", "Take medicine", NotificationActionType.Foreground),
        new NotificationAction("skip_medication", "Skip medicine", NotificationActionType.Foreground),
    })
};

Notification categories are usually registered at app startup time using the following method call:

IFirebasePushNotification.RegisterNotificationCategories(categories);
Subscribe to Notification Actions

Subscribe the event IFirebasePushNotification.NotificationAction to get notified if a user presses one of the notification action buttons. The delivered event args FirebasePushNotificationResponseEventArgs will let you know which action was pressed.

Send Notification Actions

Use the Firebase Admin SDK (or any other HTTP client) to send a push notification with:

HTTP POST https://fcm.googleapis.com/fcm/send

{
    "data": {
        "click_action": "medication_intake",
        "title": "title",
        "body": "body",
        "priority": "high",
        "id": "99"
    },
    "priority": "high",
    "registration_ids": [
        "d5xOmbOuSpSL3GytYDUptm:APA91bEhnZ5lOgACKxNJS0Tutm6M9x19hGbHkbBJ4-k7nNSiz7N9a5vYBoteRNWHFNGu6ahb51vIjzRmi2NC50V3mJkNGLpOWBA_CFbV409n1OtS2k48FtyeOdYj_HRosEQzYjqlWg0l"
    ]
}

If everything works fine, the mobile device with push notification token "d5xOmbOu...." displays the notification action as follows:

Notification Category medication_intake

Options

to be documented

Contribution

Contributors welcome! If you find a bug or you want to propose a new feature, feel free to do so by opening a new issue on github.com.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-android33.0 is compatible.  net7.0-ios was computed.  net7.0-ios16.1 is compatible.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-android34.0 is compatible.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-ios17.2 is compatible.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.56-pre 0 5/25/2024
2.2.53-pre 47 5/23/2024
2.2.51-pre 125 5/20/2024
2.2.49-pre 71 5/20/2024
2.2.48-pre 71 5/17/2024
2.2.45-pre 59 5/17/2024
2.2.44-pre 70 5/15/2024
2.2.42-pre 68 5/15/2024
2.2.37-pre 65 5/13/2024
2.2.26-pre 112 5/9/2024
2.2.17-pre 71 5/8/2024
2.2.15-pre 84 5/7/2024
2.2.13-pre 88 5/1/2024
2.2.12-pre 137 4/11/2024
2.2.11-pre 67 4/10/2024
2.2.10-pre 66 4/10/2024
2.2.9-pre 65 4/10/2024
2.2.8-pre 57 4/10/2024
2.2.7-pre 88 4/1/2024
2.2.6-pre 145 3/26/2024
2.2.2-pre 131 2/27/2024
2.1.40-pre 69 3/1/2024
2.1.39-pre 57 2/28/2024
2.1.38-pre 69 2/28/2024
2.1.33-pre 71 2/27/2024
2.1.24-pre 204 1/18/2024
2.1.15-pre 221 10/31/2023
2.1.12-pre 84 10/23/2023
2.1.9-pre 73 10/19/2023
2.1.6-pre 69 10/19/2023
2.1.3-pre 80 10/19/2023
2.0.11-pre 89 10/9/2023
2.0.6-pre 79 10/9/2023
2.0.4-pre 77 10/7/2023
1.0.27-pre 77 11/1/2023
1.0.26 1,247 10/6/2023
1.0.15-pre 89 10/2/2023
1.0.14-pre 73 10/2/2023
1.0.13-pre 89 10/1/2023
1.0.12-pre 73 10/1/2023
1.0.6-pre 80 9/30/2023
1.0.5-pre 81 9/30/2023

1.0
- Initial release