Persilsoft.WebPush.Blazor 1.0.6

dotnet add package Persilsoft.WebPush.Blazor --version 1.0.6                
NuGet\Install-Package Persilsoft.WebPush.Blazor -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.Blazor" 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.Blazor --version 1.0.6                
#r "nuget: Persilsoft.WebPush.Blazor, 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.Blazor as a Cake Addin
#addin nuget:?package=Persilsoft.WebPush.Blazor&version=1.0.6

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

Persilsoft.WebPush.Blazor

Provides a service to facilitate user subscription to the push notification service.


Example
First, you need to register the following group of services in the dependency container.

using Persilsoft.WebPush.Blazor.Options;
using ServiceCollectionExtensions;

Action<WebPushNotificationOptions> WebPushNotificationOptionsConfigurator = options =>
builder.Configuration.GetSection(WebPushNotificationOptions.SectionKey).Bind(options);
builder.Services.AddNotificationService(WebPushNotificationOptionsConfigurator);

In the configuration file appsettings.json you must configure the following key:

"WebPushNotification": {
    "ServerPublicKey": "XXXXXXXXXXXXXXXXXXXXX"
}

Where: ServerPublicKey: The VAPID public key.

Now, you can use the service to obtain a subscription:

@page "/get-subscription"
@inject WebPushService PushNotificationService

<h3>Subscription Demo</h3>

<button class="btn btn-primary" @onclick=GetSubscriptionAsync>Get Subscription</button>

@if (pushSubscription != default)
{
    <div class="mt-3 card">
        <div class="card-body">
            <div>
                <div class="mt-3">Endpoint</div>
                <div class="p-3 bg-dark text-white">@pushSubscription.Endpoint</div>
            </div>

            <div>
                <div class="mt-3">P256dh</div>
                <div class="p-3 bg-dark text-white">@pushSubscription.P256dh</div>
            </div>

            <div>
                <div class="mt-3">Auth</div>
                <div class="p-3 bg-dark text-white">@pushSubscription.Auth</div>
            </div>

            <div>
                <div class="mt-3">Is New Subscription</div>
                <div class="p-3 bg-dark text-white">@pushSubscription.IsNewSubscription</div>
            </div>
        </div>
    </div>
}

@if (!string.IsNullOrEmpty(message))
{
    <div class="m-3 alert alert-info">@message</div>
}

@code {
    private PushSubscription pushSubscription;
    string message;

    private async Task GetSubscriptionAsync()
    {
        message = string.Empty;
        pushSubscription = await PushNotificationService.GetSubscription();
        if (pushSubscription == default)
        {
            message = "Subscription could not be retrieved.";
        }
    }
}

In order for the user to receive push notifications, they will need to send the subscription data, including the public key, to the backend server.

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 125 8/18/2024
1.0.4 107 7/20/2024
1.0.3 225 5/23/2024
1.0.2 100 5/22/2024
1.0.1 273 4/24/2024
1.0.0 112 4/24/2024