Skip to content
Browse files

optional gcm_ok response to sender

  • Loading branch information...
1 parent 5106cae commit fcbc1d742fb503b3842ca8e7d526f6bda7e506ce @chvanikoff committed Apr 13, 2014
Showing with 13 additions and 6 deletions.
  1. +13 −6 src/gcm.erl
View
19 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,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]),
@@ -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) ->

0 comments on commit fcbc1d7

Please sign in to comment.
Something went wrong with that request. Please try again.