Persilsoft.WebPush.Server
1.0.2
See the version list below for details.
dotnet add package Persilsoft.WebPush.Server --version 1.0.2
NuGet\Install-Package Persilsoft.WebPush.Server -Version 1.0.2
<PackageReference Include="Persilsoft.WebPush.Server" Version="1.0.2" />
paket add Persilsoft.WebPush.Server --version 1.0.2
#r "nuget: Persilsoft.WebPush.Server, 1.0.2"
// Install Persilsoft.WebPush.Server as a Cake Addin #addin nuget:?package=Persilsoft.WebPush.Server&version=1.0.2 // Install Persilsoft.WebPush.Server as a Cake Tool #tool nuget:?package=Persilsoft.WebPush.Server&version=1.0.2
Persilsoft.WebPush.Server
This package facilitates the sending of Web-Push notifications.
Example
First, you need to register the following group of services in the dependency container.
using ServiceCollectionExtensions;
builder.Services.Configure<VapidInfoOptions>(builder.Configuration.GetSection(VapidInfoOptions.SectionKey));
builder.Services.AddWebPushService();
In the configuration file appsettings.json you must configure the following key:
"VapidInfo": {
"Subject": "mailto: <example@example.com>",
"PublicKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"PrivateKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
Where:
Subject: The mailto or URL for your application.
PublicKey: The VAPID public key.
PrivateKey: The VAPID private key.
The VAPID keys will be used to sign the push notification requests and verify their authenticity. This package uses the Elliptic Curve Diffie-Hellman P-256 asymmetric cryptography algorithm.
There are many online VAPID key generators, for example: https://www.attheminute.com/vapid-key-generator.
Now, you can send a push notification as follows:
class Process(WebPushService webPushService, IOptions<VapidInfoOptions> options, ILogger<Process> logger)
{
public void Foo()
{
var pushEndpoint = @"https://fcm.googleapis.com/fcm/send/efz_TLX_rLU:APA91bE6U0iybLYvv0F3mf6uDLB6....";
var p256dh = @"BKK18ZjtENC4jdhAAg9OfJacySQiDVcXMamy3SKKy7FwJcI5E0DKO9v4V2Pb8NnAPN4EVdmhO............";
var auth = @"fkJatBBEl...............";
var subscription = new SubscriptionInfo(pushEndpoint, p256dh, auth);
var payload = new
{
message = "Your order is on its way."
};
var serializedPayload = JsonSerializer.Serialize(payload);
var vapidInfo = new VapidInfo(
options.Value.Subject,
options.Value.PublicKey,
options.Value.PrivateKey);
try
{
await webPushService.SendNotification(subscription, serializedPayload, vapidInfo);
}
catch (Exception ex)
{
logger.LogError(ex.Message);
}
}
}
subscription: It contains the subscription detail.
serializedPayload: The payload that will be sent with a push message.
vapidInfo: It contains the subject, public key and private key. These values should follow the VAPID Spec
*Note: * The subscription data should come from the frontend and are generated by the notification services, after the user has given consent to receive push notifications. For more details on how to obtain a subscription, you can check the documentation of the Persilsoft.WebPush.Blazor package.
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.IdentityModel.Tokens (>= 7.5.1)
- Portable.BouncyCastle (>= 1.9.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.