Persilsoft.WebPush.Server
1.0.1
See the version list below for details.
dotnet add package Persilsoft.WebPush.Server --version 1.0.1
NuGet\Install-Package Persilsoft.WebPush.Server -Version 1.0.1
<PackageReference Include="Persilsoft.WebPush.Server" Version="1.0.1" />
paket add Persilsoft.WebPush.Server --version 1.0.1
#r "nuget: Persilsoft.WebPush.Server, 1.0.1"
// Install Persilsoft.WebPush.Server as a Cake Addin #addin nuget:?package=Persilsoft.WebPush.Server&version=1.0.1 // Install Persilsoft.WebPush.Server as a Cake Tool #tool nuget:?package=Persilsoft.WebPush.Server&version=1.0.1
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.
There are many VAPID key generators on the internet, 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
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.