web-push-libs/web-push-csharp

Name: web-push-csharp

Owner: web-push-libs

Description: Web Push library for C#

Created: 2016-12-21 01:28:03.0

Updated: 2018-05-23 17:28:34.0

Pushed: 2018-04-26 10:43:24.0

Homepage: null

Size: 101

Language: C#

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

?

web-push-csharp

Travis Build Status Nuget Package Details

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 Core demo project located here

Usage

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

g WebPush;

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

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

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

webPushClient = new WebPushClient();


webPushClient.SendNotification(subscription, "payload", vapidDetails);
//webPushClient.SendNotification(subscription, "payload", gcmAPIKey);

h (WebPushException exception)

Console.WriteLine("Http STATUS code" + exception.StatusCode);

API Reference

SendNotification(pushSubscription, payload, vapidDetails|gcmAPIKey|options)
subscription = new PushSubscription(pushEndpoint, p256dh, auth);

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

webPushClient = new WebPushClient();


webPushClient.SendNotification(subscription, "payload", options);

h (WebPushException exception)

Console.WriteLine("Http STATUS code" + exception.StatusCode);

Note: SendNotification() 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 containing any of the following values defined, although none of them are required.


GenerateVapidKeys()
dDetails vapidKeys = VapidHelper.GenerateVapidKeys();

rints 2 URL Safe Base64 Encoded Strings
ole.WriteLine("Public {0}", vapidKeys.PublicKey);
ole.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.


SetGCMAPIKey(apiKey)
ushClient.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.


GetVapidHeaders(audience, subject, publicKey, privateKey, expiration)
uri = new Uri(subscription.Endpoint);
ng audience = uri.Scheme + Uri.SchemeDelimiter + uri.Host;

ionary<string, string> vapidHeaders = VapidHelper.GetVapidHeaders(
dience,
mailto: example@example.com",
blicKey,
ivateKey

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:

Returns

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


Browser Support

Browser Push without Payload Push with Payload VAPID Notes
Chrome ? v42+ ? v50+ ? v52+ In v51 and less, the `gcm_sender_id` is needed to get a push subscription.
Edge ? ? ?
Firefox ? v44+ ? v44+ ? v46+
Opera ? v39+ Android *

? v42+ Desktop
? v39+ Android *

? v42+ Desktop
? v42+ Desktop * The `gcm_sender_id` is needed to get a push subscription.
Safari ? ? ?
Samsung Internet Browser ? v4.0.10-53+ ? ? The `gcm_sender_id` is needed to get a push subscription.

Help

Service Worker Cookbook

The Service Worker Cookbook is full of Web Push examples.

Credits


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.