ExNudge.Subscription (ExNudge v1.0.0)

View Source

Subscription from a client.

Summary

Functions

Creates a new subscription from a JSON. You can use metadata to pass around some form of identification of the subscription. Metadata is NOT sent with the push notification request.

Validates that a subscription has all required fields and valid format.

Types

t()

@type t() :: %ExNudge.Subscription{
  endpoint: String.t(),
  keys: %{p256dh: String.t(), auth: String.t()},
  metadata: any()
}

Functions

from_map(subscription_map, metadata \\ nil)

@spec from_map(map(), any()) :: {:ok, t()} | {:error, :invalid_subscription}

Creates a new subscription from a JSON. You can use metadata to pass around some form of identification of the subscription. Metadata is NOT sent with the push notification request.

Examples

iex> data = %{
...>   "endpoint" => "https://fcm.googleapis.com/...",
...>   "keys" => %{
...>     "p256dh" => "client_public_key",
...>     "auth" => "client_auth_secret"
...>   }
...> }
iex> ExNudge.Subscription.from_map(data)
{:ok, %ExNudge.Subscription{...}}

iex> data = %{
...>   "endpoint" => "https://fcm.googleapis.com/...",
...>   "keys" => %{
...>     "p256dh" => "client_public_key",
...>     "auth" => "client_auth_secret"
...>   }
...> }
iex> metadata = "subscription_id_1"
iex> ExNudge.Subscription.from_map(data, metadata)
{:ok, %ExNudge.Subscription{...}}

valid?(subscription)

@spec valid?(t()) :: boolean()

Validates that a subscription has all required fields and valid format.