Persilsoft.WebPush.Blazor 1.0.1

dotnet add package Persilsoft.WebPush.Blazor --version 1.0.1
NuGet\Install-Package Persilsoft.WebPush.Blazor -Version 1.0.1
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.1" />
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.1
#r "nuget: Persilsoft.WebPush.Blazor, 1.0.1"
#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.1

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

Persilsoft.WebPush.Blazor

This package 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.1 76 4/24/2024
1.0.0 75 4/24/2024