Skip to content

Commit

Permalink
allow headers to be set as part of the notification (2600hz#5827)
Browse files Browse the repository at this point in the history
  • Loading branch information
lazedo authored and jamesaimonetti committed May 31, 2019
1 parent 9a09271 commit 71329e4
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 47 deletions.
15 changes: 15 additions & 0 deletions applications/crossbar/priv/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -34160,6 +34160,11 @@
"description": "PEM-encoded key and certificate",
"type": "string"
},
"headers": {
"default": {},
"description": "pusher apple headers",
"type": "object"
},
"host": {
"default": "api.push.apple.com",
"description": "APNs server host",
Expand All @@ -34175,6 +34180,11 @@
"api_key": {
"description": "API Key for firebase",
"type": "string"
},
"headers": {
"default": {},
"description": "pusher firebase headers",
"type": "object"
}
},
"type": "object"
Expand All @@ -34186,6 +34196,11 @@
"api_key": {
"description": "API Key for gcm",
"type": "string"
},
"headers": {
"default": {},
"description": "pusher google headers",
"type": "object"
}
},
"type": "object"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"description": "PEM-encoded key and certificate",
"type": "string"
},
"headers": {
"default": {},
"description": "pusher apple headers",
"type": "object"
},
"host": {
"default": "api.push.apple.com",
"description": "APNs server host",
Expand All @@ -35,6 +40,11 @@
"api_key": {
"description": "API Key for firebase",
"type": "string"
},
"headers": {
"default": {},
"description": "pusher firebase headers",
"type": "object"
}
},
"type": "object"
Expand All @@ -46,6 +56,11 @@
"api_key": {
"description": "API Key for gcm",
"type": "string"
},
"headers": {
"default": {},
"description": "pusher google headers",
"type": "object"
}
},
"type": "object"
Expand Down
37 changes: 22 additions & 15 deletions applications/pusher/src/modules/pm_apple.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ terminate(_Reason, #state{tab=ETS}) ->
code_change(_OldVsn, State, _Extra) ->
{'ok', State}.

-spec maybe_send_push_notification(kz_term:api_pid(), kz_json:object()) -> any().
-spec maybe_send_push_notification(push_app(), kz_json:object()) -> any().
maybe_send_push_notification('undefined', _) -> 'ok';
maybe_send_push_notification(Pid, JObj) ->
maybe_send_push_notification({Pid, ExtraHeaders}, JObj) ->
TokenID = kz_json:get_value(<<"Token-ID">>, JObj),
Topic = apns_topic(JObj),
TopicArg = #{apns_topic => Topic},
Headers = kz_maps:merge(#{apns_topic => Topic}, ExtraHeaders),
Msg = build_payload(JObj),
lager:debug_unsafe("pushing topic ~s for token-id ~s : ~s", [Topic, TokenID, kz_json:encode(kz_json:from_map(Msg), ['pretty'])]),
{Result, _Props, _Ignore} = apns:push_notification(Pid, TokenID, Msg, TopicArg),
{Result, _Props, _Ignore} = apns:push_notification(Pid, TokenID, Msg, Headers),
lager:debug("apns result for ~s : ~B", [Topic, Result]).

-spec build_payload(kz_json:object()) -> map().
Expand All @@ -88,25 +88,32 @@ map_key(K, V, JObj) ->
{_, K1} -> kz_json:set_value(K1, V, JObj)
end.

-spec get_apns(kz_term:api_binary(), ets:tid()) -> kz_term:api_pid().
-spec get_apns(kz_term:api_binary(), ets:tid()) -> push_app().
get_apns('undefined', _) -> 'undefined';
get_apns(App, ETS) ->
case ets:lookup(ETS, App) of
[] -> maybe_load_apns(App, ETS);
[{App, Pid}] -> Pid
[{App, Push}] -> Push
end.

-spec maybe_load_apns(kz_term:api_binary(), ets:tid()) -> kz_term:api_pid().
-spec maybe_load_apns(kz_term:api_binary(), ets:tid()) -> push_app().
maybe_load_apns(App, ETS) ->
CertBin = kapps_config:get_ne_binary(?CONFIG_CAT, [<<"apple">>, <<"certificate">>], 'undefined', App),
Host = kapps_config:get_ne_binary(?CONFIG_CAT, [<<"apple">>, <<"host">>], ?DEFAULT_APNS_HOST, App),
maybe_load_apns(App, ETS, CertBin, Host).

-spec maybe_load_apns(kz_term:api_binary(), ets:tid(), kz_term:api_ne_binary(), kz_term:ne_binary()) -> kz_term:api_pid().
maybe_load_apns(App, _, 'undefined', _) ->
ExtraHeaders = kapps_config:get_json(?CONFIG_CAT, [<<"apple">>, <<"headers">>], kz_json:new(), App),
Headers = kz_maps:keys_to_atoms(kz_json:to_map(ExtraHeaders)),
maybe_load_apns(App, ETS, CertBin, Host, Headers).

-spec maybe_load_apns(kz_term:api_binary()
,ets:tid()
,kz_term:api_ne_binary()
,kz_term:ne_binary()
,map()
) -> push_app().
maybe_load_apns(App, _, 'undefined', _, _) ->
lager:debug("apple pusher certificate for app ~s not found", [App]),
'undefined';
maybe_load_apns(App, ETS, CertBin, Host) ->
maybe_load_apns(App, ETS, CertBin, Host, Headers) ->
{Key, Cert} = pusher_util:binary_to_keycert(CertBin),
lager:debug("starting apple push connection for ~s : ~s", [App, Host]),
Connection = #{name => kz_term:to_atom(App, 'true')
Expand All @@ -119,11 +126,11 @@ maybe_load_apns(App, ETS, CertBin, Host) ->
},
case apns:connect(Connection) of
{'ok', Pid} ->
ets:insert(ETS, {App, Pid}),
Pid;
ets:insert(ETS, {App, {Pid, Headers}}),
{Pid, Headers};
{'error', {'already_started', Pid}} ->
apns:close_connection(Pid),
maybe_load_apns(App, ETS, CertBin, Host);
maybe_load_apns(App, ETS, CertBin, Host, Headers);
{'error', Reason} ->
lager:error("error loading apns ~p", [Reason]),
'undefined'
Expand Down
36 changes: 20 additions & 16 deletions applications/pusher/src/modules/pm_firebase.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ terminate(_Reason, #state{tab=ETS}) ->
code_change(_OldVsn, State, _Extra) ->
{'ok', State}.

-spec maybe_send_push_notification(kz_term:api_pid(), kz_json:object()) -> any().
-spec maybe_send_push_notification(push_app(), kz_json:object()) -> any().
maybe_send_push_notification('undefined', _JObj) -> lager:debug("no pid to send push");
maybe_send_push_notification(Pid, JObj) ->
maybe_send_push_notification({Pid, Envelope}, JObj) ->
TokenID = kz_json:get_value(<<"Token-ID">>, JObj),
Message = kz_json:from_list([{<<"data">>, build_payload(JObj)}]),
MessageJObj = kz_json:from_list([{<<"data">>, build_payload(JObj)}]),
Message = kz_maps:merge(kz_json:to_map(MessageJObj), Envelope),

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

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

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

Expand All @@ -88,31 +89,34 @@ map_key(K, V, JObj) ->
{_, K1} -> kz_json:set_value(K1, V, JObj)
end.

-spec get_fcm(kz_term:api_binary(), ets:tid()) -> kz_term:api_pid().
-spec get_fcm(kz_term:api_binary(), ets:tid()) -> push_app().
get_fcm('undefined', _) -> 'undefined';
get_fcm(App, ETS) ->
case ets:lookup(ETS, App) of
[] -> maybe_load_fcm(App, ETS);
[{App, Pid}] -> Pid
[{App, Push}] -> Push
end.

-spec maybe_load_fcm(kz_term:api_binary(), ets:tid()) -> kz_term:api_pid().
-spec maybe_load_fcm(kz_term:api_binary(), ets:tid()) -> push_app().
maybe_load_fcm(App, ETS) ->
lager:debug("loading fcm secret for ~s", [App]),
maybe_load_fcm(App, ETS, kapps_config:get_binary(?CONFIG_CAT, [<<"firebase">>, <<"api_key">>], 'undefined', App)).
FCMSecret = kapps_config:get_binary(?CONFIG_CAT, [<<"firebase">>, <<"api_key">>], 'undefined', App),
EnvelopeJObj = kapps_config:get_json(?CONFIG_CAT, [<<"firebase">>, <<"headers">>], kz_json:new(), App),
Envelope = kz_json:to_map(EnvelopeJObj),
maybe_load_fcm(App, ETS, FCMSecret, Envelope).

-spec maybe_load_fcm(kz_term:api_binary(), ets:tid(), kz_term:api_binary()) -> kz_term:api_pid().
maybe_load_fcm(App, _, 'undefined') ->
-spec maybe_load_fcm(kz_term:api_binary(), ets:tid(), kz_term:api_binary(), map()) -> push_app().
maybe_load_fcm(App, _, 'undefined', _) ->
lager:debug("firebase pusher api_key for app ~s not found", [App]),
'undefined';
maybe_load_fcm(App, ETS, APIKey) ->
maybe_load_fcm(App, ETS, APIKey, Envelope) ->
case fcm:start(kz_term:to_atom(App, 'true'), APIKey) of
{'ok', Pid} ->
ets:insert(ETS, {App, Pid}),
Pid;
ets:insert(ETS, {App, {Pid, Envelope}}),
{Pid, Envelope};
{'error', {'already_started', Pid}} ->
ets:insert(ETS, {App, Pid}),
Pid;
ets:insert(ETS, {App, {Pid, Envelope}}),
{Pid, Envelope};
{'error', Reason} ->
lager:error("error loading fcm ~p", [Reason]),
'undefined'
Expand Down
34 changes: 19 additions & 15 deletions applications/pusher/src/modules/pm_google.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ terminate(_Reason, #state{tab=ETS}) ->
code_change(_OldVsn, State, _Extra) ->
{'ok', State}.

-spec maybe_send_push_notification(kz_term:api_pid(), kz_json:object()) -> any().
-spec maybe_send_push_notification(push_app(), kz_json:object()) -> any().
maybe_send_push_notification('undefined', _JObj) -> lager:debug("no pid to send push");
maybe_send_push_notification(Pid, JObj) ->
maybe_send_push_notification({Pid, Envelope}, JObj) ->
TokenID = kz_json:get_value(<<"Token-ID">>, JObj),
Message = kz_json:from_list([{<<"data">>, build_payload(JObj)}]),
MessageJObj = kz_json:from_list([{<<"data">>, build_payload(JObj)}]),
Message = kz_maps:merge(kz_json:to_map(MessageJObj), Envelope),

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

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

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

Expand All @@ -88,31 +89,34 @@ map_key(K, V, JObj) ->
{_, K1} -> kz_json:set_value(K1, V, JObj)
end.

-spec get_gcm(kz_term:api_binary(), ets:tid()) -> kz_term:api_pid().
-spec get_gcm(kz_term:api_binary(), ets:tid()) -> push_app().
get_gcm('undefined', _) -> 'undefined';
get_gcm(App, ETS) ->
case ets:lookup(ETS, App) of
[] -> maybe_load_gcm(App, ETS);
[{App, Pid}] -> Pid
[{App, Push}] -> Push
end.

-spec maybe_load_gcm(kz_term:api_binary(), ets:tid()) -> kz_term:api_pid().
-spec maybe_load_gcm(kz_term:api_binary(), ets:tid()) -> push_app().
maybe_load_gcm(App, ETS) ->
lager:debug("loading gcm secret for ~s", [App]),
maybe_load_gcm(App, ETS, kapps_config:get_binary(?CONFIG_CAT, [<<"google">>, <<"api_key">>], 'undefined', App)).
GCMSecret = kapps_config:get_binary(?CONFIG_CAT, [<<"google">>, <<"api_key">>], 'undefined', App),
EnvelopeJObj = kapps_config:get_json(?CONFIG_CAT, [<<"google">>, <<"headers">>], kz_json:new(), App),
Envelope = kz_json:to_map(EnvelopeJObj),
maybe_load_gcm(App, ETS, GCMSecret, Envelope).

-spec maybe_load_gcm(kz_term:api_binary(), ets:tid(), kz_term:api_binary()) -> kz_term:api_pid().
maybe_load_gcm(App, _, 'undefined') ->
-spec maybe_load_gcm(kz_term:api_binary(), ets:tid(), kz_term:api_binary(), map()) -> push_app().
maybe_load_gcm(App, _, 'undefined', _) ->
lager:debug("google pusher secret for app ~s not found", [App]),
'undefined';
maybe_load_gcm(App, ETS, Secret) ->
maybe_load_gcm(App, ETS, Secret, Envelope) ->
case gcm:start(kz_term:to_atom(App, 'true'), Secret) of
{'ok', Pid} ->
ets:insert(ETS, {App, Pid}),
Pid;
ets:insert(ETS, {App, {Pid, Envelope}}),
{Pid, Envelope};
{'error', {'already_started', Pid}} ->
ets:insert(ETS, {App, Pid}),
Pid;
ets:insert(ETS, {App, {Pid, Envelope}}),
{Pid, Envelope};
{'error', Reason} ->
lager:error("error loading gcm ~p", [Reason]),
'undefined'
Expand Down
2 changes: 2 additions & 0 deletions applications/pusher/src/pusher.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
-define(TOKEN_KEY, <<"Token-ID">>).
-define(TOKEN_PROXY_KEY, <<"Proxy-Path">>).

-type push_app() :: {kz_term:api_pid(), map()} | 'undefined'.

-define(PUSHER_HRL, 'true').
-endif.
2 changes: 1 addition & 1 deletion make/deps.mk
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ifeq ($(USER),travis)
endif

dep_amqp_client = hex 3.7.14
dep_apns = git https://github.com/inaka/apns4erl.git 2.3.0
dep_apns = git https://github.com/2600hz/erlang-apns4erl.git aba1fa96a4abbbb2c1628ad5d604f482aad4d12f # latest commit SHA to 2600hz branch

# dep_certifi = hex 0.3.0
# Used by hackney, let it pull in certifi
Expand Down

0 comments on commit 71329e4

Please sign in to comment.