diff --git a/lib/tusker_push/forwarder.ex b/lib/tusker_push/forwarder.ex index 0d43ed5..5b69bbe 100644 --- a/lib/tusker_push/forwarder.ex +++ b/lib/tusker_push/forwarder.ex @@ -4,8 +4,9 @@ defmodule TuskerPush.Forwarder do require Logger - @spec forward(Registration.t(), binary(), String.t(), String.t()) :: :ok | {:error, term()} - def forward(%Registration{push_version: 1} = registration, body, salt, key) do + @spec forward(Registration.t(), binary(), String.t(), String.t(), String.t() | nil) :: + :ok | {:error, term()} + def forward(%Registration{push_version: 1} = registration, body, salt, key, context) do payload = %{ "aps" => %{ "alert" => %{ @@ -20,6 +21,13 @@ defmodule TuskerPush.Forwarder do "v" => 1 } + payload = + unless is_nil(context) do + Map.put(payload, "ctx", context) + else + payload + end + Logger.debug("Sending #{inspect(payload)}") Apns.send(registration, payload) diff --git a/lib/tusker_push_web/controllers/push_controller.ex b/lib/tusker_push_web/controllers/push_controller.ex index 658ba43..09913c0 100644 --- a/lib/tusker_push_web/controllers/push_controller.ex +++ b/lib/tusker_push_web/controllers/push_controller.ex @@ -4,7 +4,7 @@ defmodule TuskerPushWeb.PushController do require Logger - def push(conn, %{"id" => id}) do + def push(conn, %{"id" => id} = params) do with {:registration, registration} when not is_nil(registration) <- {:registration, TuskerPush.get_registration(id)}, :ok <- TuskerPush.check_registration_expired(registration), @@ -12,7 +12,8 @@ defmodule TuskerPushWeb.PushController do {:body, {:ok, body, conn}} <- {:body, read_body(conn)}, {:salt, salt} when not is_nil(salt) <- get_salt(conn), {:key, key} when not is_nil(key) <- get_key(conn), - {:forward, :ok} <- {:forward, Forwarder.forward(registration, body, salt, key)} do + context <- Map.get(params, "context"), + {:forward, :ok} <- {:forward, Forwarder.forward(registration, body, salt, key, context)} do send_resp(conn, 200, "ok") else {:registration, nil} ->