Tingle.Extensions.PushNotifications
4.11.2
See the version list below for details.
dotnet add package Tingle.Extensions.PushNotifications --version 4.11.2
NuGet\Install-Package Tingle.Extensions.PushNotifications -Version 4.11.2
<PackageReference Include="Tingle.Extensions.PushNotifications" Version="4.11.2" />
paket add Tingle.Extensions.PushNotifications --version 4.11.2
#r "nuget: Tingle.Extensions.PushNotifications, 4.11.2"
// Install Tingle.Extensions.PushNotifications as a Cake Addin #addin nuget:?package=Tingle.Extensions.PushNotifications&version=4.11.2 // Install Tingle.Extensions.PushNotifications as a Cake Tool #tool nuget:?package=Tingle.Extensions.PushNotifications&version=4.11.2
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net7.0
- Microsoft.Extensions.FileProviders.Abstractions (>= 8.0.0)
- Tingle.Extensions.Http (>= 4.11.2)
- Tingle.Extensions.Http.Authentication (>= 4.11.2)
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 |
---|---|---|
4.13.0 | 230 | 8/13/2024 |
4.12.0 | 107 | 8/7/2024 |
4.11.2 | 208 | 7/15/2024 |
4.11.1 | 241 | 6/26/2024 |
4.11.0 | 215 | 6/6/2024 |
4.10.1 | 88 | 6/5/2024 |
4.10.0 | 152 | 5/27/2024 |
4.9.0 | 209 | 5/16/2024 |
4.8.0 | 236 | 5/5/2024 |
4.7.0 | 364 | 3/25/2024 |
4.6.0 | 295 | 3/8/2024 |
4.5.0 | 987 | 11/22/2023 |
4.4.1 | 196 | 11/20/2023 |
4.4.0 | 133 | 11/15/2023 |
4.3.0 | 364 | 10/18/2023 |