{"id":19101611,"url":"https://github.com/web-push-libs/web-push-csharp","last_synced_at":"2025-04-18T18:32:43.777Z","repository":{"id":14841074,"uuid":"77004891","full_name":"web-push-libs/web-push-csharp","owner":"web-push-libs","description":"Web Push library for C# ","archived":false,"fork":false,"pushed_at":"2024-02-06T08:41:38.000Z","size":125,"stargazers_count":433,"open_issues_count":22,"forks_count":107,"subscribers_count":29,"default_branch":"master","last_synced_at":"2025-03-15T08:09:16.443Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/web-push-libs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2016-12-21T01:28:03.000Z","updated_at":"2025-02-15T15:58:43.000Z","dependencies_parsed_at":"2024-02-06T08:48:09.067Z","dependency_job_id":"1e15b9a4-1a43-4887-b18f-dcf3e25b9d5a","html_url":"https://github.com/web-push-libs/web-push-csharp","commit_stats":{"total_commits":105,"total_committers":11,"mean_commits":9.545454545454545,"dds":0.5523809523809524,"last_synced_commit":"44d7cc5548ed35822c55af57fa276dc0a82c1285"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web-push-libs%2Fweb-push-csharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web-push-libs%2Fweb-push-csharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web-push-libs%2Fweb-push-csharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web-push-libs%2Fweb-push-csharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/web-push-libs","download_url":"https://codeload.github.com/web-push-libs/web-push-csharp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249482128,"owners_count":21279821,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T03:53:22.731Z","updated_at":"2025-04-18T18:32:43.763Z","avatar_url":"https://github.com/web-push-libs.png","language":"C#","funding_links":[],"categories":["others"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eweb-push-csharp\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/web-push-libs/web-push-csharp/actions/workflows/CI.yml\"\u003e\n    \u003cimg src=\"https://github.com/web-push-libs/web-push-csharp/actions/workflows/CI.yml/badge.svg\" alt=\"CI Build\" /\u003e\t\n  \u003c/a\u003e\n  \u003ca href=\"https://www.nuget.org/packages/WebPush/\"\u003e\n    \u003cimg src=\"https://buildstats.info/nuget/WebPush\" alt=\"Nuget Package Details\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n# Why\n\nWeb push requires that push messages triggered from a backend be done via the\n[Web Push Protocol](https://tools.ietf.org/html/draft-ietf-webpush-protocol)\nand if you want to send data with your push message, you must also encrypt\nthat data according to the [Message Encryption for Web Push spec](https://tools.ietf.org/html/draft-ietf-webpush-encryption).\n\nThis package makes it easy to send messages and will also handle legacy support\nfor browsers relying on GCM for message sending / delivery.\n\n# Install\n\nInstallation is simple, just install via NuGet.\n\n    Install-Package WebPush\n\n# Demo Project\n\nThere is a ASP.NET MVC Core demo project located [here](https://github.com/coryjthompson/WebPushDemo)\n\n# Usage\n\nThe common use case for this library is an application server using\na GCM API key and VAPID keys.\n\n```csharp\nusing WebPush;\n\nvar pushEndpoint = @\"https://fcm.googleapis.com/fcm/send/efz_TLX_rLU:APA91bE6U0iybLYvv0F3mf6uDLB6....\";\nvar p256dh = @\"BKK18ZjtENC4jdhAAg9OfJacySQiDVcXMamy3SKKy7FwJcI5E0DKO9v4V2Pb8NnAPN4EVdmhO............\";\nvar auth = @\"fkJatBBEl...............\";\n\nvar subject = @\"mailto:example@example.com\";\nvar publicKey = @\"BDjASz8kkVBQJgWcD05uX3VxIs_gSHyuS023jnBoHBgUbg8zIJvTSQytR8MP4Z3-kzcGNVnM...............\";\nvar privateKey = @\"mryM-krWj_6IsIMGsd8wNFXGBxnx...............\";\n\nvar subscription = new PushSubscription(pushEndpoint, p256dh, auth);\nvar vapidDetails = new VapidDetails(subject, publicKey, privateKey);\n//var gcmAPIKey = @\"[your key here]\";\n\nvar webPushClient = new WebPushClient();\ntry\n{\n\tawait webPushClient.SendNotificationAsync(subscription, \"payload\", vapidDetails);\n    //await webPushClient.SendNotificationAsync(subscription, \"payload\", gcmAPIKey);\n}\ncatch (WebPushException exception)\n{\n\tConsole.WriteLine(\"Http STATUS code\" + exception.StatusCode);\n}\n```\n\n# API Reference\n\n## SendNotificationAsync(pushSubscription, payload, vapidDetails|gcmAPIKey|options, cancellationToken)\n\n```csharp\nvar subscription = new PushSubscription(pushEndpoint, p256dh, auth);\n\nvar options = new Dictionary\u003cstring,object\u003e();\noptions[\"vapidDetails\"] = new VapidDetails(subject, publicKey, privateKey);\n//options[\"gcmAPIKey\"] = @\"[your key here]\";\n\nvar webPushClient = new WebPushClient();\ntry\n{\n\twebPushClient.SendNotificationAsync(subscription, \"payload\", options);\n}\ncatch (WebPushException exception)\n{\n\tConsole.WriteLine(\"Http STATUS code\" + exception.StatusCode);\n}\n```\n\n\u003e **Note:** `SendNotificationAsync()` you don't need to define a payload, and this\nmethod will work without a GCM API Key and / or VAPID keys if the push service\nsupports it.\n\n### Input\n\n**Push Subscription**\n\nThe first argument must be an PushSubscription object containing the details for a push\nsubscription.\n\n**Payload**\n\nThe payload is optional, but if set, will be the data sent with a push\nmessage.\n\nThis must be a *string*\n\u003e **Note:** In order to encrypt the *payload*, the *pushSubscription* **must**\nhave a *keys* object with *p256dh* and *auth* values.\n\n**Options**\n\nOptions is an optional argument that if defined should be an Dictionary\u003cstring,object\u003e containing\nany of the following values defined, although none of them are required.\n\n- **gcmAPIKey** can be a GCM API key to be used for this request and this\nrequest only. This overrides any API key set via `setGCMAPIKey()`.\n- **vapidDetails** should be a VapidDetails object with *subject*, *publicKey* and\n*privateKey* values defined. These values should follow the [VAPID Spec](https://tools.ietf.org/html/draft-thomson-webpush-vapid).\n- **TTL** is a value in seconds that describes how long a push message is\nretained by the push service (by default, four weeks).\n- **headers** is an object with all the extra headers you want to add to the request.\n\n\u003chr /\u003e\n\n## GenerateVapidKeys()\n\n```csharp\nVapidDetails vapidKeys = VapidHelper.GenerateVapidKeys();\n\n// Prints 2 URL Safe Base64 Encoded Strings\nConsole.WriteLine(\"Public {0}\", vapidKeys.PublicKey);\nConsole.WriteLine(\"Private {0}\", vapidKeys.PrivateKey);\n```\n\n### Input\n\nNone.\n\n### Returns\n\nReturns a VapidDetails object with **PublicKey** and **PrivateKey** values populated which are\nURL Safe Base64 encoded strings.\n\n\u003e **Note:** You should create these keys once, store them and use them for all\n\u003e future messages you send.\n\n\u003chr /\u003e\n\n## SetGCMAPIKey(apiKey)\n\n```csharp\nwebPushClient.SetGCMAPIKey(@\"your-gcm-key\");\n```\n\n### Input\n\nThis method expects the GCM API key that is linked to the `gcm_sender_id ` in\nyour web app manifest.\n\nYou can use a GCM API Key from the Google Developer Console or the\n*Cloud Messaging* tab under a Firebase Project.\n\n### Returns\n\nNone.\n\n\u003chr /\u003e\n\n## GetVapidHeaders(audience, subject, publicKey, privateKey, expiration)\n\n```csharp\nUri uri = new Uri(subscription.Endpoint);\nstring audience = uri.Scheme + Uri.SchemeDelimiter + uri.Host;\n\nDictionary\u003cstring, string\u003e vapidHeaders = VapidHelper.GetVapidHeaders(\n  audience,\n  @\"mailto: example@example.com\",\n  publicKey,\n  privateKey\n);\n```\n\nThe *GetVapidHeaders()* method will take in the values needed to create\nan Authorization and Crypto-Key header.\n\n### Input\n\nThe `GetVapidHeaders()` method expects the following input:\n\n- *audience*: the origin of the **push service**.\n- *subject*: the mailto or URL for your application.\n- *publicKey*: the VAPID public key.\n- *privateKey*: the VAPID private key.\n\n### Returns\n\nThis method returns a Dictionary\u003cstring, string\u003e intented to be headers of a web request. It will contain the following keys:\n\n- *Authorization*\n- *Crypto-Key*.\n\n\u003chr /\u003e\n\n# Browser Support\n\n\u003ctable\u003e\n\u003cthead\u003e\n\u003ctr\u003e\n\t\u003cth\u003e\u003cstrong\u003eBrowser\u003c/strong\u003e\u003c/th\u003e\n    \u003cth width=\"130px\"\u003e\u003cstrong\u003ePush without Payload\u003c/strong\u003e\u003c/th\u003e\n    \u003cth width=\"130px\"\u003e\u003cstrong\u003ePush with Payload\u003c/strong\u003e\u003c/th\u003e\n    \u003cth width=\"130px\"\u003e\u003cstrong\u003eVAPID\u003c/strong\u003e\u003c/th\u003e\n    \u003cth\u003e\u003cstrong\u003eNotes\u003c/strong\u003e\u003c/th\u003e\n\u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n\u003ctr\u003e\n\t\u003ctd\u003eChrome\u003c/td\u003e\n\t\u003c!-- Push without payloads support--\u003e\n   \u003ctd\u003e✓ v42+\u003c/td\u003e\n   \u003c!-- Push with payload support --\u003e\n   \u003ctd\u003e✓ v50+\u003c/td\u003e\n   \u003c!-- VAPID Support --\u003e\n   \u003ctd\u003e✓ v52+\u003c/td\u003e\n   \u003ctd\u003eIn v51 and less, the `gcm_sender_id` is needed to get a push subscription.\u003c/td\u003e\n   \u003c/tr\u003e\n\n   \u003ctr\u003e\n   \u003ctd\u003eFirefox\u003c/td\u003e\n\n   \u003c!-- Push without payloads support--\u003e\n   \u003ctd\u003e✓ v44+\u003c/td\u003e\n\n   \u003c!-- Push with payload support --\u003e\n   \u003ctd\u003e✓ v44+\u003c/td\u003e\n\n   \u003c!-- VAPID Support --\u003e\n   \u003ctd\u003e✓ v46+\u003c/td\u003e\n\n   \u003ctd\u003e\u003c/td\u003e\n   \u003c/tr\u003e\n\n   \u003ctr\u003e\n   \u003ctd\u003eOpera\u003c/td\u003e\n\n   \u003c!-- Push without payloads support--\u003e\n   \u003ctd\u003e✓ v39+ Android \u003cstrong\u003e*\u003c/strong\u003e\n       \u003cbr/\u003e\n       \u003cbr/\u003e\n       ✓ v42+ Desktop\n\u003c/td\u003e\n   \u003c!-- Push with payload support --\u003e\n   \u003ctd\u003e✓ v39+ Android \u003cstrong\u003e*\u003c/strong\u003e\n       \u003cbr/\u003e\n       \u003cbr/\u003e\n       ✓ v42+ Desktop\n\u003c/td\u003e\n\n   \u003c!-- VAPID Support --\u003e\n   \u003ctd\u003e✓ v42+ Desktop\u003c/td\u003e\n\n   \u003ctd\u003e\n   \u003cstrong\u003e*\u003c/strong\u003e The `gcm_sender_id` is needed to get a push subscription.\n   \u003c/td\u003e\n   \u003c/tr\u003e\n\n   \u003ctr\u003e\n   \u003ctd\u003eEdge\u003c/td\u003e\n\n   \u003c!-- Push without payloads support--\u003e\n   \u003ctd\u003e✓ v17+\u003c/td\u003e\n\n   \u003c!-- Push with payload support --\u003e\n   \u003ctd\u003e✓ v17+\u003c/td\u003e\n\n   \u003c!-- VAPID Support --\u003e\n   \u003ctd\u003e✓ v17+\u003c/td\u003e\n\n   \u003ctd\u003e\u003c/td\u003e\n   \u003c/tr\u003e\n   \u003ctr\u003e\n   \u003ctd\u003eSafari\u003c/td\u003e\n\n   \u003c!-- Push without payloads support--\u003e\n   \u003ctd\u003e✗\u003c/td\u003e\n\n   \u003c!-- Push with payload support --\u003e\n   \u003ctd\u003e✗\u003c/td\u003e\n\n   \u003c!-- VAPID Support --\u003e\n   \u003ctd\u003e✗\u003c/td\u003e\n\n   \u003ctd\u003e\u003c/td\u003e\n   \u003c/tr\u003e\n\n   \u003ctr\u003e\n   \u003ctd\u003eSamsung Internet Browser\u003c/td\u003e\n   \u003c!-- Push without payloads support--\u003e\n   \u003ctd\u003e✓ v4.0.10-53+\u003c/td\u003e\n   \u003c!-- Push with payload support --\u003e\n   \u003ctd\u003e✗\u003c/td\u003e\n\n   \u003c!-- VAPID Support --\u003e\n   \u003ctd\u003e✗\u003c/td\u003e\n\n   \u003ctd\u003eThe `gcm_sender_id` is needed to get a push subscription.\u003c/td\u003e\n   \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n# Help\n\n**Service Worker Cookbook**\n\nThe [Service Worker Cookbook](https://serviceworke.rs/) is full of Web Push\nexamples.\n\n# Credits\n- Ported from https://github.com/web-push-libs/web-push.\n- Original Encryption code from https://github.com/LogicSoftware/WebPushEncryption\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweb-push-libs%2Fweb-push-csharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweb-push-libs%2Fweb-push-csharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweb-push-libs%2Fweb-push-csharp/lists"}