Skip to content

Commit

Permalink
optional gcm_ok response to sender
Browse files Browse the repository at this point in the history
  • Loading branch information
chvanikoff committed Apr 13, 2014
1 parent 5106cae commit fcbc1d7
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/gcm.erl
Expand Up @@ -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}).
Expand Down Expand Up @@ -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) ->
Expand Down Expand Up @@ -155,15 +158,15 @@ code_change(_OldVsn, State, _Extra) ->
%%%===================================================================
%%% Internal functions
%%%===================================================================
do_push(RegIds, Message, Key, ErrorFun) ->
do_push(RegIds, Message, Key, ErrorFun, From) ->
lager:info("Message=~p; RegIds=~p~n", [Message, RegIds]),
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]),
Expand Down Expand Up @@ -197,7 +200,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) ->
Expand Down

0 comments on commit fcbc1d7

Please sign in to comment.