WebPush 1.0.13

dotnet add package WebPush --version 1.0.13
                    
NuGet\Install-Package WebPush -Version 1.0.13
                    
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="WebPush" Version="1.0.13" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebPush" Version="1.0.13" />
                    
Directory.Packages.props
<PackageReference Include="WebPush" />
                    
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 WebPush --version 1.0.13
                    
#r "nuget: WebPush, 1.0.13"
                    
#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 WebPush@1.0.13
                    
#: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=WebPush&version=1.0.13
                    
Install as a Cake Addin
#tool nuget:?package=WebPush&version=1.0.13
                    
Install as a Cake Tool

<h1 align="center">web-push-csharp</h1>

CI Build Version Downloads

Why

Web push requires that push messages triggered from a backend be done via the Web Push Protocol and if you want to send data with your push message, you must also encrypt that data according to the Message Encryption for Web Push spec.

This package makes it easy to send messages and will also handle legacy support for browsers relying on GCM for message sending / delivery.

Install

Installation is simple, just install via NuGet.

Install-Package WebPush

Demo Project

There is a ASP.NET MVC demo project located here

Usage

The common use case for this library is an application server using a GCM API key and VAPID keys.

using WebPush;

var pushEndpoint = @"https://fcm.googleapis.com/fcm/send/efz_TLX_rLU:APA91bE6U0iybLYvv0F3mf6uDLB6....";
var p256dh = @"BKK18ZjtENC4jdhAAg9OfJacySQiDVcXMamy3SKKy7FwJcI5E0DKO9v4V2Pb8NnAPN4EVdmhO............";
var auth = @"fkJatBBEl...............";

var subject = @"mailto:example@example.com";
var publicKey = @"BDjASz8kkVBQJgWcD05uX3VxIs_gSHyuS023jnBoHBgUbg8zIJvTSQytR8MP4Z3-kzcGNVnM...............";
var privateKey = @"mryM-krWj_6IsIMGsd8wNFXGBxnx...............";

var subscription = new PushSubscription(pushEndpoint, p256dh, auth);
var vapidDetails = new VapidDetails(subject, publicKey, privateKey);
//var gcmAPIKey = @"[your key here]";

var webPushClient = new WebPushClient();
try
{
	await webPushClient.SendNotificationAsync(subscription, "payload", vapidDetails);
    //await webPushClient.SendNotificationAsync(subscription, "payload", gcmAPIKey);
}
catch (WebPushException exception)
{
	Console.WriteLine("Http STATUS code" + exception.StatusCode);
}

API Reference

SendNotificationAsync(pushSubscription, payload, vapidDetails|gcmAPIKey|options, cancellationToken)

var subscription = new PushSubscription(pushEndpoint, p256dh, auth);

var options = new Dictionary<string,object>();
options["vapidDetails"] = new VapidDetails(subject, publicKey, privateKey);
//options["gcmAPIKey"] = @"[your key here]";

var webPushClient = new WebPushClient();
try
{
	webPushClient.SendNotificationAsync(subscription, "payload", options);
}
catch (WebPushException exception)
{
	Console.WriteLine("Http STATUS code" + exception.StatusCode);
}

Note: SendNotificationAsync() you don't need to define a payload, and this method will work without a GCM API Key and / or VAPID keys if the push service supports it.

Input

Push Subscription

The first argument must be an PushSubscription object containing the details for a push subscription.

Payload

The payload is optional, but if set, will be the data sent with a push message.

This must be a string

Note: In order to encrypt the payload, the pushSubscription must have a keys object with p256dh and auth values.

Options

Options is an optional argument that if defined should be an Dictionary<string,object> containing any of the following values defined, although none of them are required.

  • gcmAPIKey can be a GCM API key to be used for this request and this request only. This overrides any API key set via setGCMAPIKey().
  • vapidDetails should be a VapidDetails object with subject, publicKey and privateKey values defined. These values should follow the VAPID Spec.
  • TTL is a value in seconds that describes how long a push message is retained by the push service (by default, four weeks).
  • headers is an object with all the extra headers you want to add to the request.

<hr />

GenerateVapidKeys()

VapidDetails vapidKeys = VapidHelper.GenerateVapidKeys();

// Prints 2 URL Safe Base64 Encoded Strings
Console.WriteLine("Public {0}", vapidKeys.PublicKey);
Console.WriteLine("Private {0}", vapidKeys.PrivateKey);

Input

None.

Returns

Returns a VapidDetails object with PublicKey and PrivateKey values populated which are URL Safe Base64 encoded strings.

Note: You should create these keys once, store them and use them for all future messages you send.

<hr />

SetGCMAPIKey(apiKey)

webPushClient.SetGCMAPIKey(@"your-gcm-key");

Input

This method expects the GCM API key that is linked to the gcm_sender_id in your web app manifest.

You can use a GCM API Key from the Google Developer Console or the Cloud Messaging tab under a Firebase Project.

Returns

None.

<hr />

GetVapidHeaders(audience, subject, publicKey, privateKey, expiration)

Uri uri = new Uri(subscription.Endpoint);
string audience = uri.Scheme + Uri.SchemeDelimiter + uri.Host;

Dictionary<string, string> vapidHeaders = VapidHelper.GetVapidHeaders(
  audience,
  @"mailto: example@example.com",
  publicKey,
  privateKey
);

The GetVapidHeaders() method will take in the values needed to create an Authorization and Crypto-Key header.

Input

The GetVapidHeaders() method expects the following input:

  • audience: the origin of the push service.
  • subject: the mailto or URL for your application.
  • publicKey: the VAPID public key.
  • privateKey: the VAPID private key.

Returns

This method returns a Dictionary<string, string> intented to be headers of a web request. It will contain the following keys:

  • Authorization
  • Crypto-Key.

<hr />

Browser Support

<table> <thead> <tr> <th><strong>Browser</strong></th> <th width="130px"><strong>Push without Payload</strong></th> <th width="130px"><strong>Push with Payload</strong></th> <th width="130px"><strong>VAPID</strong></th> <th><strong>Notes</strong></th> </tr> </thead> <tbody> <tr> <td>Chrome</td>

<td>✓ v42+</td>

<td>✓ v50+</td>

<td>✓ v52+</td> <td>In v51 and less, the gcm_sender_id is needed to get a push subscription.</td> </tr>

<tr> <td>Firefox</td>

<td>✓ v44+</td>

<td>✓ v44+</td>

<td>✓ v46+</td>

<td></td> </tr>

<tr> <td>Opera</td>

<td>✓ v39+ Android <strong>*</strong> <br/> <br/> ✓ v42+ Desktop </td>

<td>✓ v39+ Android <strong>*</strong> <br/> <br/> ✓ v42+ Desktop </td>

<td>✓ v42+ Desktop</td>

<td> <strong>*</strong> The gcm_sender_id is needed to get a push subscription. </td> </tr>

<tr> <td>Edge</td>

<td>✓ v17+</td>

<td>✓ v17+</td>

<td>✓ v17+</td>

<td></td> </tr> <tr> <td>Safari</td>

<td>✗</td>

<td>✗</td>

<td>✗</td>

<td></td> </tr>

<tr> <td>Samsung Internet Browser</td>

<td>✓ v4.0.10-53+</td>

<td>✗</td>

<td>✗</td>

<td>The gcm_sender_id is needed to get a push subscription.</td> </tr> </tbody> </table>

Help

Service Worker Cookbook

The Service Worker Cookbook is full of Web Push examples.

Credits

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on WebPush:

Package Downloads
Aj.Platform.Core

Biblioteca de Uso Geral para integrações com as API OmsAj

DanSaul.SharedCode

Package Description

cloudscribe.PwaKit

A set of tools for building a dynamic serviceworker at runtime for ASP.NET Core

PushServer.WebPushClientAdapter

Adapter to use WebPush client from https://github.com/web-push-libs/web-push-csharp with PushServer framework for ASP.NET Core 2.

LEC.Services.WebPush

Package Description

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on WebPush:

Repository Stars
notifo-io/notifo
Multi channel notification service for collaboration tools, e-commerce, news service and more.
AiursoftWeb/Kahla
Kahla is a cross-platform business messaging app. Mirror of https://gitlab.aiursoft.com/aiursoft/kahla
Valour-Software/Valour
Valour is bringing communities into the future with unique features, blazing performance, and respect for users.
Version Downloads Last Updated
1.0.13 6,428 4/28/2026
1.0.12 3,256,098 7/3/2021
1.0.11 1,547,226 3/30/2018
1.0.10 16,970 2/10/2018
1.0.9 60,412 4/7/2017
1.0.8 43,273 1/23/2017
1.0.7 2,027 1/17/2017
1.0.6 2,002 1/5/2017
1.0.5 2,284 1/2/2017
1.0.4 2,042 12/27/2016
1.0.3 3,096 12/23/2016
1.0.2 2,014 12/23/2016
1.0.1 2,493 12/23/2016
1.0.0 3,382 12/23/2016