### Install WebPushEx Dependency Source: https://hexdocs.pm/web_push_ex/readme.html Add the library to your mix.exs file to include it in your project. ```elixir def deps do [ {:web_push_ex, "~> 0.1.0"} ] end ``` -------------------------------- ### request(subscription, message, options) Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Builds and returns a Request struct ready for sending a push notification. ```APIDOC ## request(subscription, message, options) ### Description Builds and returns a Request struct containing the necessary details for sending a push notification. ### Parameters - **subscription** (WebPushEx.Subscription.t) - Required - The subscription details. - **message** (String.t) - Required - The message to be sent. - **options** (keyword) - Optional - Additional configuration options. ### Returns - **WebPushEx.Request.t** - The constructed request struct. ``` -------------------------------- ### Construct and Send Web Push Request Source: https://hexdocs.pm/web_push_ex/readme.html Create a WebPushEx.Request struct from subscription data and send it using an HTTP client. ```elixir # create the struct from the subscription JSON data subscription = WebPushEx.Subscription.from_json(""" {"endpoint":"https://push.example.com/123","keys":{"p256dh":"user_agent_public_key","auth":"auth_secret"}} """) # structured message, see example serviceWorker.js linked below message = %{title: "Notification Title", body: "lorem ipsum etc"} # generate request details %WebPushEx.Request{} = request = WebPushEx.request(subscription, :json.encode(message) |> to_string()) request.endpoint # => "https://push.example.com/123" request.body # => binary data request.headers # => %{ # "Authorization" => "vapid t=..., k=...", # "Content-Encoding" => "aes128gcm", # "Content-Length" => "42", # "Content-Type" => "application/octet-stream", # "TTL" => "43200", # "Urgency" => "normal" # } # send web push notification via http client e.g. req {:ok, resp} = Req.post(request.endpoint, body: request.body, headers: request.headers) ``` -------------------------------- ### Generate VAPID keys via Mix Source: https://hexdocs.pm/web_push_ex/Mix.Tasks.WebPushEx.Vapid.html Executes the mix task to generate VAPID keys for project configuration. ```bash $ mix web_push.vapid ``` -------------------------------- ### Build request struct Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Builds and returns a Request struct ready for sending. ```elixir @spec request(WebPushEx.Subscription.t(), String.t(), keyword()) :: WebPushEx.Request.t() ``` -------------------------------- ### Configure VAPID Keys Source: https://hexdocs.pm/web_push_ex/readme.html Generate VAPID keys using the mix task and configure them in your application's configuration files. ```elixir $ mix web_push.vapid # in config/config.exs: config :web_push_ex, :vapid, public_key: "" subject: "webpush-admin-email@example.com" # in config/runtime.exs: config :web_push_ex, :vapid, private_key: System.fetch_env!("WEB_PUSH_VAPID_PRIVATE_KEY") # in your environment: export WEB_PUSH_EX_VAPID_PRIVATE_KEY= ``` -------------------------------- ### Define input_keying_material type Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Represents the input keying material binary type. ```elixir @type input_keying_material() :: binary() ``` -------------------------------- ### Subscribe to Push Notifications Source: https://hexdocs.pm/web_push_ex/readme.html Use the VAPID public key when calling the browser's pushManager.subscribe method. ```javascript pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: vapidPublicKey, }).then(...); ``` -------------------------------- ### from_json(json) Source: https://hexdocs.pm/web_push_ex/WebPushEx.Subscription.html Parses a JSON string into a WebPushEx.Subscription struct. ```APIDOC ## from_json(json) ### Description Builds and returns a new WebPushEx.Subscription struct from a provided JSON string. ### Signature `from_json(String.t()) :: t()` ### Parameters - **json** (String.t) - Required - A JSON string containing the subscription data. ``` -------------------------------- ### Define key type Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Represents the key binary type. ```elixir @type key() :: binary() ``` -------------------------------- ### encode(binary) Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Encodes a binary into a URL-safe Base64 string with padding disabled. ```APIDOC ## encode(binary) ### Description URL-safe Base64 encodes a binary with padding disabled. ### Parameters - **binary** (binary) - Required - The binary data to encode. ### Returns - **String.t** - The URL-safe Base64 encoded string. ``` -------------------------------- ### Define info type Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Represents the info binary type. ```elixir @type info() :: binary() ``` -------------------------------- ### Encode binary to URL-safe Base64 Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Encodes a binary to a URL-safe Base64 string without padding. ```elixir @spec encode(binary()) :: String.t() ``` -------------------------------- ### Parse Subscription from JSON Source: https://hexdocs.pm/web_push_ex/WebPushEx.Subscription.html Specifies the function signature for converting a JSON string into a Subscription struct. ```elixir @spec from_json(String.t()) :: t() ``` -------------------------------- ### Define headers type Source: https://hexdocs.pm/web_push_ex/WebPushEx.Request.html Represents the map of headers required for the POST request. ```elixir @type headers() :: %{required(String.t()) => String.t()} ``` -------------------------------- ### Define psuedo_random_key type Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Represents the pseudo-random key binary type. ```elixir @type psuedo_random_key() :: binary() ``` -------------------------------- ### Define endpoint type Source: https://hexdocs.pm/web_push_ex/WebPushEx.Request.html Represents the URI destination for the POST request. ```elixir @type endpoint() :: URI.t() ``` -------------------------------- ### decode(string) Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Decodes a URL-safe Base64-encoded string with padding disabled. ```APIDOC ## decode(string) ### Description Decodes a URL-safe Base64-encoded string with padding disabled. ### Parameters - **string** (String.t) - Required - The Base64 encoded string to decode. ### Returns - **binary** - The decoded binary data. ``` -------------------------------- ### Decode URL-safe Base64 string Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Decodes a URL-safe Base64-encoded string without padding. ```elixir @spec decode(String.t()) :: binary() ``` -------------------------------- ### Define Request struct type Source: https://hexdocs.pm/web_push_ex/WebPushEx.Request.html The main struct type containing the body, endpoint, and headers for a push request. ```elixir @type t() :: %WebPushEx.Request{ body: body(), endpoint: endpoint(), headers: headers() } ``` -------------------------------- ### Define Subscription Type Source: https://hexdocs.pm/web_push_ex/WebPushEx.Subscription.html Represents the structure of a subscription containing an endpoint URI and encryption keys. ```elixir @type t() :: %WebPushEx.Subscription{ endpoint: URI.t(), keys: %{p256dh: String.t(), auth: String.t()} } ``` -------------------------------- ### Define body type Source: https://hexdocs.pm/web_push_ex/WebPushEx.Request.html Represents the encrypted content encoding body as a binary. ```elixir @type body() :: binary() ``` -------------------------------- ### Define salt type Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Represents the salt binary type. ```elixir @type salt() :: binary() ``` -------------------------------- ### Define vapid_key type Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Represents the VAPID key atom types. ```elixir @type vapid_key() :: :private_key | :public_key | :subject ``` -------------------------------- ### encrypt_payload(payload, subscription, options) Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Encrypts a message payload according to RFCs 8291, 8188, and 5869. ```APIDOC ## encrypt_payload(payload, subscription, options) ### Description Encrypts a message payload for a given subscription using the specified options. ### Parameters - **payload** (String.t) - Required - The message content to encrypt. - **subscription** (WebPushEx.Subscription.t) - Required - The subscription details. - **options** (keyword) - Optional - Additional configuration options. ### Returns - **binary** - The encrypted payload. ``` -------------------------------- ### Define length type Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Represents the length as a non-negative integer. ```elixir @type length() :: non_neg_integer() ``` -------------------------------- ### Encrypt payload Source: https://hexdocs.pm/web_push_ex/WebPushEx.html Encrypts a message according to RFCs 8291, 8188, and 5869. ```elixir @spec encrypt_payload(String.t(), WebPushEx.Subscription.t(), keyword()) :: binary() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.