Vapid.NET 0.2.1

dotnet add package Vapid.NET --version 0.2.1
                    
NuGet\Install-Package Vapid.NET -Version 0.2.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="Vapid.NET" Version="0.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vapid.NET" Version="0.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Vapid.NET" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Vapid.NET --version 0.2.1
                    
#r "nuget: Vapid.NET, 0.2.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.
#:package Vapid.NET@0.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Vapid.NET&version=0.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Vapid.NET&version=0.2.1
                    
Install as a Cake Tool

Vapid.NET icon

Vapid.NET

A minimal, no-dependency Vapid WebPush client for .NET.

Motivation

The original/most popular WebPush client for .NET has multiple issues (in my opinion):

  • It uses the Portable.BouncyCastle package.
    • This package was made before .NET had built-in Cryptography functions that WebPush requires.
    • There is a pull request that tries to fix this, however, this code uses Windows-only functions and hasn't been updated to resolve this issue.
  • It uses the Newtonsoft.Json package.
    • Again, this package was made before .NET had efficient built-in JSON parsing.
    • The latest release has finally fixed this (after 5 years).
  • It can allocate quite some data in memory.
  • You are almost required to write a wrapper around this package to make it more developer-friendly.

Features

  • Lightweight package that has little to no dependencies
  • Faster than web-push-csharp when creating HTTP requests.
    • The speed of this library is almost completely dependent on the speed of the WebPush server.
  • Declarative Web Push support.
  • Clean and developer-friendly API.

Missing features

  • No GCM support.
    • Even though GCM support seems redundant, an application might still need it.

Usage

ASP.NET Core: Server

  1. Register and configure the Vapid client for DI:
services.AddVapidClient((options) =>
{
	// You can generate this information on the following website:
	// https://vapidkeys.com

	options.Subject = "…"; // Most of the time, this is an `mailto:…` address.
	options.PublicKey = "…";
	options.PrivateKey = "…";
});
  1. Send a push notification:
using Vapid.NET;
using Vapid.NET.Models;

// Don't forget to inject the `VapidClient`.

// You'd probably store and fetch this from a database:
var pushSubscription = new PushSubscription
{
	Endpoint = "…",
	P256dh = "…",
	Auth = "…",
};

var notification = new PushNotification
{
	Title = "Vapid.NET",
	Body = "Hello from the server!",
	Navigate = "https://git.byrone.nl/byrone/Vapid.NET",
};

// `SendAsync` returns a `SendResult` indicating if the push notification was sent, or if any action must be undertaken.
var result = await client.SendAsync(pushSubscription, notification, /*optional:*/ cancellationToken);

ASP.NET Core: Client

Push notifications are sent using the Declarative Web Push format. But not a lot of browsers support this format (yet). You should make sure your application has a service worker that handles the push event:

self.addEventListener('push', (event) => event.waitUntil(showNotification(event)));
self.addEventListener('notificationclick', (event) => event.waitUntil(onNotificationClick(event)));

// Fallback in case the browser doesn't support Declarative Web Push.
async function showNotification(event) {
	if (!event.data) {
		throw new Error('Received push event without any data.');
	}

	const data = await event.data.json();
	const notification = data.notification;

	await self.registration.showNotification(notification.title, {
		icon: '/icon-512.png',
		...notification,
		// Safari is (at the time of writing this) the only browser that supports the `navigate` property when showing a notification.
		// Not only do other browsers not use this property, the value is entirely discared when calling the `notificationclick` event.
		// Hence we need to pass the `navigate` value via the `data` property, you can add additional properties to this object if needed.
		data: {
			navigate: notification.navigate
		},
	});
}

// Fallback in case the browser didn't recognize the `navigate` key.
async function onNotificationClick(event) {
	event.notification.close()

	const notification = event.notification;

	const url = notification.data.navigate;

	if (!url) {
		return;
	}

	const windows = await clients.matchAll({ type: 'window' });

	for (const client of windows) {
		if (client.url === url && 'focus' in client) {
			return await client.focus();
		}
	}

	if (clients.openWindow) {
		return clients.openWindow(url);
	}
}
Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
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
0.2.1 81 10/2/2026
0.2.0 236 8/21/2026
0.1.0 97 3/31/2026