Persilsoft.WebPush.Server 1.0.6

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

// Install Persilsoft.WebPush.Server as a Cake Tool
#tool nuget:?package=Persilsoft.WebPush.Server&version=1.0.6                

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 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
1.0.6 90 10/16/2024
1.0.5 110 7/23/2024
1.0.4 125 7/20/2024
1.0.3 120 5/22/2024
1.0.2 132 4/24/2024
1.0.1 118 4/24/2024
1.0.0 115 4/24/2024