Tingle.Extensions.PushNotifications 5.0.0

dotnet add package Tingle.Extensions.PushNotifications --version 5.0.0                
NuGet\Install-Package Tingle.Extensions.PushNotifications -Version 5.0.0                
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="Tingle.Extensions.PushNotifications" Version="5.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tingle.Extensions.PushNotifications --version 5.0.0                
#r "nuget: Tingle.Extensions.PushNotifications, 5.0.0"                
#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 Tingle.Extensions.PushNotifications as a Cake Addin
#addin nuget:?package=Tingle.Extensions.PushNotifications&version=5.0.0

// Install Tingle.Extensions.PushNotifications as a Cake Tool
#tool nuget:?package=Tingle.Extensions.PushNotifications&version=5.0.0                

Tingle.Extensions.PushNotifications

This library contains lightweight clients for sending push notifications to devices via APNS and FCM. It exists either because there is no comprehensive library or the official library cannot be tested by stubbing HTTP requests

Apple Push Notification Service (APNs)

This library is a wrapper around the service, you still need to understand how the service works in order to make correct requests. Make sure to read the official docs.

In your Program.cs:

builder.Services.AddApnsNotifier(options =>
{
   options.TeamId = builder.Configuration["Apns:TeamId"];
   options.KeyId = builder.Configuration["Apns:KeyId"];
   options.BundleId = builder.Configuration["Apns:BundleId"];

   options.UsePrivateKey(keyId => environment.ContentRootFileProvider.GetFileInfo($"AuthKey_{keyId}.p8"));
});

In your appsettings.json (or any other configuration store/source you use):

{
   "Apns:TeamId": "AA0A0AAAA0",
   "Apns:BundleId": "com.apple.iBooks",
   "Apns:KeyId": "AA00AA000A",
   // ....
}

You can also choose to provide the private key directly:

builder.Services.AddApnsNotifier(options =>
{
   options.TeamId = builder.Configuration["Apns:TeamId"];
   options.KeyId = builder.Configuration["Apns:KeyId"];
   options.BundleId = builder.Configuration["Apns:BundleId"];

   options.PrivateKeyBytes = (keyId) => File.ReadAllBytes($"apns-key.p8");
});

In a sample service (named NotificationManager.cs below):

private readonly IHostEnvironment environment;
private readonly ApnsNotifier apnsNotifier;

public NotificationManager(IHostEnvironment environment, ApnsNotifier apnsNotifier)
{
   this.environment = environment ?? throw new ArgumentNullException(nameof(environment));
   this.apnsNotifier = apnsNotifier ?? throw new ArgumentNullException(nameof(apnsNotifier));
}

async Task SendApnsAsync(CancellationToken cancellationToken = default)
{
   var header = new ApnsMessageHeader
   {
      CollapseId = request.Collapse,
      Environment = environment.IsProduction() ? ApnsEnvironment.Production : ApnsEnvironment.Development,
      PushType = ApnsPushType.Background,
      DeviceToken = "<device-token-here>",
   };

   // According to Listing 7-1 Configuring a background update notification,
   // we should set content-available = 1 if the other properties of aps are not set.
   // https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
   var aps = new ApnsMessagePayload { ContentAvailable = 1, };
   var data = new ApnsMessageData(aps);

   await apnsNotifier.SendAsync(header, data, cancellationToken);
}

Firebase (FCM)

This library is a wrapper around the service, you still need to understand how the service works in order to make correct requests. Make sure to read the official docs.

V1 HTTP API

In your appsettings.json (or any other configuration store/source you use):

{
   "Firebase:ProjectId": "dummy-id",
   // ....
}

In Program.cs add the following code snippet:

builder.Services.AddFirebaseNotifier(options =>
{
   var projectId = configuration.GetRequiredValue<string>("Firebase:ProjectId");
   options.UseConfigurationFromFile(environment.ContentRootFileProvider.GetFileInfo($"{projectId}.json"));
});

// ...

You can also choose to provide the configuration details directly:

builder.Services.AddFirebaseNotifier(options =>
{
   options.ProjectId = builder.Configuration["Firebase:ProjectId"];
   options.ClientEmail = builder.Configuration["Firebase:ClientEmail"];
   options.TokenUri = builder.Configuration["Firebase:TokenUri"];
   options.PrivateKey = builder.Configuration["Firebase:PrivateKey"];
});

In a sample service (named NotificationManager.cs below):

private readonly IHostEnvironment environment;
private readonly FirebaseNotifier firebaseNotifier;

public NotificationManager(IHostEnvironment environment, FirebaseNotifier firebaseNotifier)
{
   this.environment = environment ?? throw new ArgumentNullException(nameof(environment));
   this.firebaseNotifier = firebaseNotifier ?? throw new ArgumentNullException(nameof(firebaseNotifier));
}

async Task SendFirebaseAsync(CancellationToken cancellationToken = default)
{
   var message = new FirebaseRequestMessage
   {
      Token = "<token-here>",
      Android = new FirebaseMessageAndroid
      {
         // add title, body, etc here
         CollapseKey = null,
      },
      Data = new Dictionary<string, string>
      {
         ["key1"] = "something here",
      },
   };

   // send the push notification
   await firebaseNotifier.SendAsync(message, cancellationToken);
}

Legacy HTTP API

In your Program.cs:

builder.Services.AddFcmLegacyNotifier(options =>
{
   options.Key = builder.Configuration["Firebase:Key"];
});

In your appsettings.json (or any other configuration store/source you use):

{
   "Firebase:Key": "<your-legacy-key-here>",
   // ....
}

In a sample service (named NotificationManager.cs below):

private readonly IHostEnvironment environment;
private readonly FcmLegacyNotifier firebaseNotifier;

public NotificationManager(IHostEnvironment environment, FcmLegacyNotifier firebaseNotifier)
{
   this.environment = environment ?? throw new ArgumentNullException(nameof(environment));
   this.firebaseNotifier = firebaseNotifier ?? throw new ArgumentNullException(nameof(firebaseNotifier));
}

async Task SendFirebaseAsync(CancellationToken cancellationToken = default)
{
   var message = new FcmLegacyRequest
   {
      RegistrationIds = ["<token1-here>","<token2-here>"],
      Data = new Dictionary<string, string>
      {
         ["key1"] = "something here",
      },
   };

   // send the push notification
   await firebaseNotifier.SendAsync(message, cancellationToken);
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  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
5.0.0 70 11/19/2024
4.14.1 215 10/14/2024
4.14.0 252 9/16/2024
4.13.0 400 8/13/2024
4.12.0 159 8/7/2024
4.11.2 234 7/15/2024
4.11.1 266 6/26/2024
4.11.0 241 6/6/2024
4.10.1 121 6/5/2024
4.10.0 161 5/27/2024
4.9.0 232 5/16/2024
4.8.0 261 5/5/2024
4.7.0 384 3/25/2024
4.6.0 317 3/8/2024
4.5.0 1,016 11/22/2023
4.4.1 221 11/20/2023
4.4.0 160 11/15/2023
4.3.0 391 10/18/2023