Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 14 additions and 7 deletions.
  1. +1 −0  src/gcm.app.src
  2. +13 −7 src/gcm.erl
View
1  src/gcm.app.src
@@ -6,6 +6,7 @@
kernel,
stdlib,
inets,
+ ssl,
jsx,
lager
]},
View
20 src/gcm.erl
@@ -49,8 +49,11 @@ start_link(Name, Key, ErrorFun) ->
stop(Name) ->
gen_server:call(Name, stop).
+
push(Name, RegIds, Message) ->
- gen_server:cast(Name, {send, RegIds, Message}).
+ push(Name, RegIds, Message, undefined).
+push(Name, RegIds, Message, From) ->
+ gen_server:cast(Name, {send, RegIds, Message, From}).
sync_push(Name, RegIds, Message) ->
gen_server:call(Name, {send, RegIds, Message}).
@@ -107,8 +110,8 @@ handle_call(_Request, _From, State) ->
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
-handle_cast({send, RegIds, Message}, #state{key=Key, error_fun=ErrorFun} = State) ->
- do_push(RegIds, Message, Key, ErrorFun),
+handle_cast({send, RegIds, Message, From}, #state{key=Key, error_fun=ErrorFun} = State) ->
+ do_push(RegIds, Message, Key, ErrorFun, From),
{noreply, State};
handle_cast(_Msg, State) ->
@@ -155,15 +158,14 @@ code_change(_OldVsn, State, _Extra) ->
%%%===================================================================
%%% Internal functions
%%%===================================================================
-do_push(RegIds, Message, Key, ErrorFun) ->
- lager:info("Message=~p; RegIds=~p~n", [Message, RegIds]),
+do_push(RegIds, Message, Key, ErrorFun, From) ->
GCMRequest = jsx:encode([{<<"registration_ids">>, RegIds}|Message]),
ApiKey = string:concat("key=", Key),
try httpc:request(post, {?BASEURL, [{"Authorization", ApiKey}], "application/json", GCMRequest}, [], []) of
{ok, {{_, 200, _}, Headers, GCMResponse}} ->
Json = jsx:decode(response_to_binary(GCMResponse)),
- handle_push_result(Json, RegIds, ErrorFun);
+ handle_push_result(Json, RegIds, ErrorFun, From);
{error, Reason} ->
%% Some general error during the request.
lager:error("error in request: ~p~n", [Reason]),
@@ -197,7 +199,11 @@ handle_push_result(Json, RegIds, ErrorFun) ->
true ->
parse_results(Results, RegIds, ErrorFun);
false ->
- ok
+ case From of
+ undefined -> ok;
+ %% TODO: implement msg IDs; keep recent(?) messages in ETS(?)
+ Pid -> Pid ! gcm_ok
+ end
end.
response_to_binary(Json) when is_binary(Json) ->

No commit comments for this range

Something went wrong with that request. Please try again.