View Source Pigeon.ADM (Pigeon v1.6.3)

Amazon Device Messaging (ADM)

Summary

Types

Async callback for push notifications response.

Options for sending push notifications.

Functions

Sends a push over ADM.

Starts ADM worker connection with given config or name.

Stops existing ADM worker connection.

Types

@type connection_response() :: {:ok, pid()} | {:error, {:already_started, pid()}}
@type on_response() :: (Pigeon.ADM.Notification.t() -> no_return())

Async callback for push notifications response.

Examples

handler = fn(%Pigeon.ADM.Notification{response: response}) ->
  case response do
    :success ->
      Logger.debug "Push successful!"
    :unregistered ->
      Logger.error "Bad device token!"
    _error ->
      Logger.error "Some other error happened."
  end
end

n = Pigeon.ADM.Notification.new("token", %{"message" => "test"})
Pigeon.ADM.push(n, on_response: handler)
@type push_opts() :: [to: atom() | pid() | nil, on_response: on_response() | nil]

Options for sending push notifications.

  • :to - Defines worker to process push. Defaults to :adm_default
  • :on_response - Optional async callback triggered on receipt of push. See on_response/0

Functions

Link to this function

push(notifications, opts \\ [])

View Source

Sends a push over ADM.

Examples

iex> msg = %{"body" => "your message"}
iex> n = Pigeon.ADM.Notification.new("your_reg_id", msg)
iex> Pigeon.ADM.push(n, on_response: nil)
:ok

iex> msg = %{"body" => "your message"}
iex> n = Pigeon.ADM.Notification.new("your_reg_id", msg)
iex> Pigeon.ADM.push(n)
%Pigeon.ADM.Notification{consolidation_key: nil,
 expires_after: 604800, md5: "M13RuG4uDWqajseQcCiyiw==",
 payload: %{"data" => %{"body" => "your message"}},
 registration_id: "your_reg_id", response: :invalid_registration_id,
 updated_registration_id: nil}

iex> msg = %{"body" => "your message"}
iex> n = Pigeon.ADM.Notification.new("your_reg_id", msg)
iex> notifs = Pigeon.ADM.push([n, n], to: :adm_default)
iex> Enum.map(notifs, & &1.response)
[:invalid_registration_id, :invalid_registration_id]

iex> me = self()
iex> handler = fn(_x) -> send(me, "Sent a push!") end
iex> n = Pigeon.ADM.Notification.new("your_reg_id", %{})
iex> Pigeon.ADM.push(n, on_response: handler)
iex> receive do
...>   x -> x
...> after
...>   5_000 -> "No push response..."
...> end
"Sent a push!"

iex> msg = %{"body" => "your message"}
iex> n = Pigeon.ADM.Notification.new("your_reg_id", msg)
iex> notif = Pigeon.ADM.push(n, to: :worker_not_started)
iex> notif.response
:timeout
@spec start_connection(atom() | Pigeon.ADM.Config.t() | Keyword.t()) ::
  connection_response()

Starts ADM worker connection with given config or name.

Examples

iex> config = Pigeon.ADM.Config.new(:adm_default)
iex> {:ok, pid} = Pigeon.ADM.start_connection(%{config | name: nil})
iex> Process.alive?(pid)
true
@spec stop_connection(atom() | pid()) :: :ok

Stops existing ADM worker connection.

Examples

iex> config = Pigeon.ADM.Config.new(:adm_default)
iex> {:ok, pid} = Pigeon.ADM.start_connection(%{config | name: nil})
iex> Pigeon.ADM.stop_connection(pid)
:ok
iex> :timer.sleep(500)
iex> Process.alive?(pid)
false