Skip to content

Commit

Permalink
add notification map to google & firebase (2600hz#5515)
Browse files Browse the repository at this point in the history
* add notification map to google & firebase

* restart apns connection if it exists

* preventing 'not_connection_owner' result
  • Loading branch information
lazedo authored and k-anderson committed Feb 15, 2019
1 parent 2db2dd4 commit b6cb684
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions applications/pusher/src/modules/pm_apple.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ maybe_load_apns(App, ETS, CertBin, Host) ->
ets:insert(ETS, {App, Pid}),
Pid;
{'error', {'already_started', Pid}} ->
ets:insert(ETS, {App, Pid}),
Pid;
apns:close_connection(Pid),
maybe_load_apns(App, ETS, CertBin, Host);
{'error', Reason} ->
lager:error("error loading apns ~p", [Reason]),
'undefined'
Expand Down
22 changes: 20 additions & 2 deletions applications/pusher/src/modules/pm_firebase.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
-record(state, {tab :: ets:tid()}).
-type state() :: #state{}.

-define(FIREBASE_MAP, [{<<"Alert-Key">>, [<<"alert">>, <<"loc-key">>]}
,{<<"Alert-Params">>, [<<"alert">>, <<"loc-args">>]}
,{<<"Sound">>, [<<"sound">>]}
,{<<"Call-ID">>, [<<"Call-ID">>]}
,{<<"Payload">>, fun kz_json:merge/2}
]).

-spec start_link() -> kz_types:startlink_ret().
start_link() ->
gen_server:start_link({'local', ?SERVER}, ?MODULE, [],[]).
Expand Down Expand Up @@ -63,13 +70,24 @@ code_change(_OldVsn, State, _Extra) ->
maybe_send_push_notification('undefined', _JObj) -> lager:debug("no pid to send push");
maybe_send_push_notification(Pid, JObj) ->
TokenID = kz_json:get_value(<<"Token-ID">>, JObj),
CallId = kz_json:get_value(<<"Call-ID">>, JObj),
Message = kz_json:from_list([{<<"data">>,kz_json:from_list([{<<"Call-ID">>, CallId}])}]),
Message = kz_json:from_list([{<<"data">>, build_payload(JObj)}]),

lager:debug("pushing to ~p: ~s: ~p", [Pid, TokenID, Message]),

fcm:push(Pid, [TokenID], kz_json:to_map(Message)).

-spec build_payload(kz_json:object()) -> map().
build_payload(JObj) ->
kz_json:foldl(fun map_key/3, kz_json:new(), JObj).

-spec map_key(term(), term(), kz_json:object()) -> kz_json:object().
map_key(K, V, JObj) ->
case lists:keyfind(K, 1, ?FIREBASE_MAP) of
'false' -> JObj;
{_, Fun} when is_function(Fun, 2) -> Fun(V, JObj);
{_, K1} -> kz_json:set_value(K1, V, JObj)
end.

-spec get_fcm(kz_term:api_binary(), ets:tid()) -> kz_term:api_pid().
get_fcm('undefined', _) -> 'undefined';
get_fcm(App, ETS) ->
Expand Down
22 changes: 20 additions & 2 deletions applications/pusher/src/modules/pm_google.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
-record(state, {tab :: ets:tid()}).
-type state() :: #state{}.

-define(GOOGLE_MAP, [{<<"Alert-Key">>, [<<"alert">>, <<"loc-key">>]}
,{<<"Alert-Params">>, [<<"alert">>, <<"loc-args">>]}
,{<<"Sound">>, [<<"sound">>]}
,{<<"Call-ID">>, [<<"Call-ID">>]}
,{<<"Payload">>, fun kz_json:merge/2}
]).

-spec start_link() -> kz_types:startlink_ret().
start_link() ->
gen_server:start_link({'local', ?SERVER}, ?MODULE, [],[]).
Expand Down Expand Up @@ -63,13 +70,24 @@ code_change(_OldVsn, State, _Extra) ->
maybe_send_push_notification('undefined', _JObj) -> lager:debug("no pid to send push");
maybe_send_push_notification(Pid, JObj) ->
TokenID = kz_json:get_value(<<"Token-ID">>, JObj),
CallId = kz_json:get_value(<<"Call-ID">>, JObj),
Message = kz_json:from_list([{<<"data">>,kz_json:from_list([{<<"Call-ID">>, CallId}])}]),
Message = kz_json:from_list([{<<"data">>, build_payload(JObj)}]),

lager:debug("pushing to ~p: ~s: ~p", [Pid, TokenID, Message]),

gcm:push(Pid, [TokenID], Message).

-spec build_payload(kz_json:object()) -> map().
build_payload(JObj) ->
kz_json:foldl(fun map_key/3, kz_json:new(), JObj).

-spec map_key(term(), term(), kz_json:object()) -> kz_json:object().
map_key(K, V, JObj) ->
case lists:keyfind(K, 1, ?GOOGLE_MAP) of
'false' -> JObj;
{_, Fun} when is_function(Fun, 2) -> Fun(V, JObj);
{_, K1} -> kz_json:set_value(K1, V, JObj)
end.

-spec get_gcm(kz_term:api_binary(), ets:tid()) -> kz_term:api_pid().
get_gcm('undefined', _) -> 'undefined';
get_gcm(App, ETS) ->
Expand Down

0 comments on commit b6cb684

Please sign in to comment.