Skip to content

Ejabberd with apns4erl

Balázs Galambosi edited this page Feb 28, 2016 · 7 revisions

This is to share my experience with ejabberd and apns4erl

  • Thanks to apns4erl team for being so awesome and supportive.

  • About me - An engineer who dreams.

  • I had an objective to send APNS (iOS) push notifications to my users DIRECTLY from ejabberd.

  • I had zero experience with erlang and ejabberd and had to struggle a lot.

  • But the book Programming Erlang by Joel Armstrong helped me a lot.

To send apns push notifications from ejabberd, following needs to be done:

  • Ejabberd offline_message_hook needs to be written
  • Basically, we need to tell ejabberd that we want it to run our hook as well whenever it sees an offline message
  • Once you follow the link above, then we need to plug-in apns4erl into ejabberd.
  • apns4erl is an erlang application which can be seamlessly used with other erlang modules and applications
  • To get it working, my init function in my hook looked like:
init([Host, Opts]) ->
  ejabberd_hooks:add(offline_message_hook, Host, ?MODULE,
                     send_apns, 50),
  lager:info("Running my module successfully"),
  Now = lists:flatten(io_lib:format("~p", [calendar:local_time()])),
  ok = apns:start(),
  {StatusCon, Pid} = apns:connect(?TEST_CONNECTION, fun log_error/2, fun log_feedback/1),
  case StatusCon of
    ok ->
      error_logger:error_msg("Successfully Started connection", []);
    _ ->
      error_logger:error_msg("Not Successfully Started connection", [])
  end,
  Ref = erlang:monitor(process, Pid),
  apns:send_message(
     ?TEST_CONNECTION, ?DEVICE_TOKEN,
     Now ++ " - Test Alert", random:uniform(10), "chime"),
  receive
    {'DOWN', Ref, _, _, _} = DownMsg ->
      throw(DownMsg);
    DownMsg ->
      throw(DownMsg)
    after 1000 ->
      ok
  end,
  {ok, {}}.

At the time of writing this article, I was not fully done with the project but I still wanted to share before I forget.

Clone this wiki locally