WebPushElixir (web_push_elixir v0.8.0)

Copy Markdown

Module to send web push notifications with an encrypted payload.

Summary

Functions

Sends a web push notification with an encrypted payload.

Functions

send_notification(subscription, message, opts \\ [])

Sends a web push notification with an encrypted payload.

Arguments

  • subscription - the subscription JSON string received from the client
  • message - the message string to send
  • opts - Optional message options, currently:
    • ttl: time to live in seconds if the client is disconnected, defaults to 60 seconds
    • urgency: Urgency of the message, one of: :very-low, :low, :normal, :high
    • topic: a string with a topic, that is used to replace previous same topic notifications

Examples

case WebPushElixir.send_notification(subscription, "Hello!") do
  {:ok, _response} ->
    :ok

  {:error, :expired} ->
    Repo.delete(subscription)

  {:error, {:http_error, status, body}} ->
    Logger.error("HTTP error #{status}: #{body}")
end

Return Values

  • {:ok, response} - notification sent successfully (HTTP 200-202)
  • {:error, :expired} - subscription expired/not found (HTTP 404 or 410)
  • {:error, {:http_error, status, body}} - HTTP error from push service