loading
Warning:
Permission prompt and prerequisites Subscribing requires a secure context, an active service worker registration and the user granting the notification permission - the browser shows its permission prompt on the first Subscribe call. If the permission is denied, Subscribe rejects with a NotAllowedError.
Note:
You also need a server The browser only hands you a subscription (endpoint plus encryption keys). Actually delivering a push requires an application server that holds the matching VAPID private key and POSTs encrypted payloads to the endpoint - for example via the web-push npm package or WebPush NuGet package. This page demonstrates the client side only.

Check support

IsSupported

Returns true when the runtime exposes ServiceWorkerRegistration.pushManager. Chromium and Firefox support push broadly; Safari supports it from 16.1 on macOS, and on iOS only for web apps added to the Home Screen.

C#
var supported = await push.IsSupported();
Live sample
support output
Results will appear here when you interact with the samples.

Prerequisite: register the demo worker

ServiceWorker.Register

Push subscriptions belong to a service worker registration. This button registers the demo /sw.js, whose push handler shows a notification with the received payload.

@inject Bit.Butil.ServiceWorker serviceWorker

<button @onclick="RegisterWorker">Register /sw.js</button>

@code {
    private async Task RegisterWorker() => await serviceWorker.Register("/sw.js");
}
Live sample
service worker output
Results will appear here when you interact with the samples.

Subscribe

Subscribe

Creates a push subscription for the active service worker. The applicationServerKey is your VAPID public key (base64-url encoded); generate a key pair once on your machine and keep the private key on your server. userVisibleOnly defaults to true because Chromium requires every push to result in a user-visible notification.

# Once per application, not once per user. The public half goes to the browser as the
# applicationServerKey; the private half stays on your server and signs every push you send.
npx web-push generate-vapid-keys
Live sample
subscribe output
Results will appear here when you interact with the samples.

Inspect the existing subscription

GetSubscription

Returns the current subscription for the active service worker, or a payload with IsActive set to false when none exists. Send the Endpoint, P256dh and Auth values to your application server - that is everything it needs to push to this browser.

C#
var sub = await push.GetSubscription();

if (sub.IsActive)
{
    // persist sub.Endpoint, sub.P256dh and sub.Auth on your server
}
Live sample
subscription output
Results will appear here when you interact with the samples.

Unsubscribe

Unsubscribe

Deactivates the active subscription and returns true when one was removed. Remember to also delete the subscription record from your application server, or it will keep pushing into the void.

C#
var removed = await push.Unsubscribe();
Live sample
unsubscribe output
Results will appear here when you interact with the samples.

API reference

Member
Signature
Description
IsSupported
ValueTask<bool> IsSupported()
True when the runtime exposes ServiceWorkerRegistration.pushManager.
GetSubscription
ValueTask<PushSubscriptionInfo> GetSubscription()
Returns the existing subscription for the active service worker, or an inactive payload when none exists.
Subscribe
ValueTask<PushSubscriptionInfo> Subscribe(string applicationServerKey, bool userVisibleOnly = true)
Subscribes the current service worker to push messages using the given VAPID public key.
Unsubscribe
ValueTask<bool> Unsubscribe()
Unsubscribes the active subscription. Returns true if a subscription was removed.
PushSubscriptionInfo.IsActive
bool IsActive { get; set; }
True when a subscription was found or created.
PushSubscriptionInfo.Endpoint
string Endpoint { get; set; }
The endpoint URL the push service expects POSTs at.
PushSubscriptionInfo.ExpirationTime
long? ExpirationTime { get; set; }
Unix-epoch milliseconds expiration time, or null when the subscription doesn't expire.
PushSubscriptionInfo.P256dh
string P256dh { get; set; }
Base64URL-encoded P-256 ECDH public key for payload encryption.
PushSubscriptionInfo.Auth
string Auth { get; set; }
Base64URL-encoded auth secret used by the Web Push protocol.
An unhandled error has occurred. Reload 🗙