Crate web_push

source ·
Expand description

Web Push

A library for creating and sending push notifications to a web browser. For content payload encryption it uses the Encrypted Content-Encoding for HTTP, draft 3. The client is asynchronious and uses Tokio with futures.

Example

extern crate tokio;
extern crate web_push;
extern crate base64;
extern crate futures;

use web_push::*;
use base64::URL_SAFE;
use futures::{Future, future::lazy};

let endpoint = "https://updates.push.services.mozilla.com/wpush/v1/...";
let p256dh = base64::decode_config("key_from_browser_as_base64", URL_SAFE).unwrap();
let auth = base64::decode_config("auth_from_browser_as_base64", URL_SAFE).unwrap();

let subscription_info = SubscriptionInfo::new(
    endpoint,
    "BLMbF9ffKBiWQLCKvTHb6LO8Nb6dcUh6TItC455vu2kElga6PQvUmaFyCdykxY2nOSSL3yKgfbmFLRTUaGv4yV8",
    "xS03Fi5ErfTNH_l9WHE9Ig"
);

let mut builder = WebPushMessageBuilder::new(&subscription_info).unwrap();
let content = "Encrypted payload to be sent in the notification".as_bytes();
builder.set_payload(ContentEncoding::AesGcm, content);

match builder.build() {
   Ok(message) => {
       let client = WebPushClient::new().unwrap();

       tokio::run(lazy(move || {
           client
               .send(message)
               .map(|_| {
                   println!("OK");
               }).map_err(|error| {
                   println!("ERROR: {:?}", error)
               })
           }));
   },
   Err(error) => {
       println!("ERROR in building message: {:?}", error)
   }
}

Structs

Client info for sending the notification. Maps the values from browser’s subscription info JSON data.
Encryption keys from the client.
A struct representing a VAPID signature. Should be generated using the VapidSignatureBuilder.
A VAPID signature builder for generating an optional signature to the request. With a given signature, one can pass the registration to Google’s FCM service. And prevent unauthorized notifications to be sent to the client.
An async client for sending the notification payload.
Everything needed to send a push notification to the user.
The main class for creating a notification payload.
The push content payload, already in an encrypted form.
The response future. When successful, returns an empty Unit for failures gives a WebPushError.

Enums