Pigeon.ADM (Pigeon v2.0.0-rc.0) View Source

Pigeon.Adapter for ADM (Amazon Android) push notifications.

Getting Started

  1. Create an ADM dispatcher.
# lib/adm.ex
defmodule YourApp.ADM do
  use Pigeon.Dispatcher, otp_app: :your_app
end
  1. (Optional) Add configuration to your config.exs.
# config.exs

config :your_app, YourApp.ADM,
  adapter: Pigeon.ADM,
  client_id: "your_oauth2_client_id_here",
  client_secret: "your_oauth2_client_secret_here"
  1. Start your dispatcher on application boot.
defmodule YourApp.Application do
  @moduledoc false

  use Application

  @doc false
  def start(_type, _args) do
    children = [
      YourApp.ADM
    ]
    opts = [strategy: :one_for_one, name: YourApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

If you skipped step two, include your configuration.

defmodule YourApp.Application do
  @moduledoc false

  use Application

  @doc false
  def start(_type, _args) do
    children = [
      {YourApp.ADM, adm_opts()}
    ]
    opts = [strategy: :one_for_one, name: YourApp.Supervisor]
    Supervisor.start_link(children, opts)
  end

  defp adm_opts do
    [
      adapter: Pigeon.ADM, 
      client_id: "client_id", 
      client_secret: "secret"
    ]
  end
end
  1. Create a notification.
msg = %{ "body" => "your message" }
n = Pigeon.ADM.Notification.new("your device registration ID", msg)
  1. Send the notification.
YourApp.ADM.push(n)

Handling Push Responses

  1. Pass an optional anonymous function as your second parameter.
data = %{ message: "your message" }
n = Pigeon.ADM.Notification.new("device registration ID", data)
YourApp.ADM.push(n, on_response: fn(x) -> IO.inspect(x) end)
  1. Responses return a notification with an updated :response key. You could handle responses like so:
on_response_handler = fn(x) ->
  case x.response do
    :success ->
      # Push successful
      :ok
    :update ->
      new_reg_id = x.updated_registration_id
      # Update the registration ID in the database
    :invalid_registration_id ->
      # Remove the bad ID from the database
    :unregistered ->
      # Remove the bad ID from the database
    error ->
      # Handle other errors
  end
end

data = %{ message: "your message" }
n = Pigeon.ADM.Notification.new("your registration id", data)
Pigeon.ADM.push(n, on_response: on_response_handler)

Error Responses

Taken from Amazon Device Messaging docs

ReasonDescription
:invalid_registration_idInvalid Registration Token
:invalid_dataBad format JSON data
:invalid_consolidation_keyInvalid Consolidation Key
:invalid_expirationInvalid expiresAfter value
:invalid_checksumInvalid md5 value
:invalid_typeInvalid Type header
:unregisteredApp instance no longer available
:access_token_expiredExpired OAuth access token
:message_too_largeData size exceeds 6 KB
:max_rate_exceededSee Retry-After response header
:unknown_errorUnknown Error