defmodule APNS do use Application defmodule Message do defmodule Loc do defstruct [ title: "", body: "", title_loc_key: nil, title_loc_args: nil, action_loc_key: nil, loc_key: "", loc_args: [], launch_image: nil ] end defstruct [ id: nil, expiry: 86400000, token: "", content_available: nil, alert: "", badge: nil, sound: "default", priority: 10, extra: [], support_old_ios: nil ] def new do {_, _, ms} = :os.timestamp s = :calendar.datetime_to_gregorian_seconds :calendar.universal_time f = rem s, 65536 l = rem ms, 65536 new <> end def new(id), do: %__MODULE__{id: id} end def start(env) when env in [:dev, :prod] do APNS.Connection.Supervisor.start(env) end def stop(pid) do Supervisor.terminate_child(APNS.Connection.Supervisor, pid) end def push(conn, token, alert) do msg = APNS.Message.new |> Map.put(:token, token) |> Map.put(:alert, alert) push(conn, msg) end def push(conn, %APNS.Message{} = msg) do APNS.Connection.Worker.push(conn, msg) end # See http://elixir-lang.org/docs/stable/elixir/Application.html # for more information on OTP Applications def start(_type, _args) do import Supervisor.Spec, warn: false children = [ # Define workers and child supervisors to be supervised # worker(APNS.Worker, [arg1, arg2, arg3]), supervisor(APNS.Connection.Supervisor, []) ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: APNS.Supervisor] Supervisor.start_link(children, opts) end defmodule Error do defstruct [ message_id: nil, status: nil, error: nil ] @statuses %{ 0 => "No errors encountered", 1 => "Processing", 2 => "Missing device token", 3 => "Missing topic", 4 => "Missing payload", 5 => "Invalid token size", 6 => "Invalid topic size", 7 => "Invalid payload size", 8 => "Invalid token", 10 => "Shutdown", 255 => "None (unknown)" } def new(msg_id, status) do %__MODULE__{message_id: msg_id, status: status, error: @statuses[status]} end end defmodule Feedback do defstruct [ time: nil, token: nil ] end def tst do {:ok, c} = start :prod t = "8d7ba39ba21203066eb1e549df9cebcf6a22ac179ff310f5afec22ea7e2f2e96" push c, t, "qweqwe" end end defmodule Tst do use GenServer require Logger @payload_max_old 256 @payload_max_new 2048 def start do start_link end def start_link() do GenServer.start_link(__MODULE__, [], []) end def init(_) do GenServer.cast(self, :listen) send(self, :listen) opts = [ certfile: "test/fixtures/cert/cert.pem", keyfile: "test/fixtures/cert/key.pem", password: 'apns4ex', mode: :binary ] {:ok, lsocket} = :ssl.listen(2195, opts) state = %{ lsocket: lsocket, buffer: "" } {:ok, state} end def handle_cast(:listen, state) do {:ok, socket} = :ssl.transport_accept(state.lsocket) accept = :ssl.ssl_accept(socket) GenServer.cast(self, {:accept, accept}) {:noreply, state} end def handle_cast({:accept, msg}, state) do IO.inspect "accepting..." IO.inspect msg {:noreply, state} end def handle_info(msg, state) do IO.inspect msg {:noreply, state} end end