| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
PushSharp.WebPush is a .NET library for sending Web Push notifications from backend servers, implementing the Web Push Protocol and Message Encryption for Web Push (VAPID, ECDH, AES-GCM). It supports legacy GCM for older browsers and is compatible with modern browsers and push services.
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 via NuGet:
Install-Package AlphaOmega.PushSharp.WebPush
The common use case for this library is an application server using VAPID keys (recommended) or a GCM API key (deprecated).
using PushSharp.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: vapidDetails);
// await webPushClient.SendNotificationAsync(subscription, "payload", gcmAPIKey: gcmAPIKey);
}
catch (WebPushException exception)
{
Console.WriteLine("Http STATUS code" + exception.StatusCode);
}Task SendNotificationAsync(PushSubscription subscription, string payload = null, VapidDetails vapidDetails = null, string gcmAPIKey = null, CancellationToken cancellationToken = default);Note: 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.
VapidDetails vapidKeys = VapidHelper.GenerateVapidKeys();
Console.WriteLine($"Public {vapidKeys.PublicKey}");
Console.WriteLine($"Private {vapidKeys.PrivateKey}");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
);webPushClient.GcmApiKey = "your-gcm-key";Contributions are welcome! Please open issues or pull requests. See CONTRIBUTING.md if available.
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).
| Back | FazBrowse Home | New Git URL |