Ejabberd- Push notifications APNS and GCM with mod

2019-09-05 23:32发布

问题:

I'm trying to get push notifications working with ejabberd and I found this link here: https://github.com/diamondbybold/ejabberd_mod_offline_post where it should make a POST request at a URL for offline messages. I edited my ejabberd.yml file to include the URL and token, however, I'm running into some issues when trying to run it on Ejabberd 16.12. It runs, but I send a message to someone offline, I'm seeing the following error in /var/log/ejabberd/error.log

2017-02-07 09:12:19.359 [error] <0.1045.0>@ejabberd_hooks:run1:332 
{function_clause,[{fxml,get_tag_attr_s,[<<"type">>,{message,
<<"purple130d74a2">>,chat,<<>>,undefined,{jid,<<"test2">>,<<"localhost">>,
<<"Smack">>,<<"test2">>,<<"localhost">>,<<"Smack">>},[],[{text,<<>>,
<<"abc">>}],undefined,[{xmlel,<<"active">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/chatstates">>}],[]}],#{}}],[{file,"src/fxml.erl"},
{line,166}]},{mod_offline_post,offline_message,3,
[{file,"src/mod_offline_post.erl"},{line,86}]},{ejabberd_hooks,safe_apply,3,
[{file,"src/ejabberd_hooks.erl"},{line,382}]},{ejabberd_hooks,run1,3,
[{file,"src/ejabberd_hooks.erl"},{line,329}]},{ejabberd_sm,route,3,
[{file,"src/ejabberd_sm.erl"},{line,126}]},{ejabberd_local,route,3,
[{file,"src/ejabberd_local.erl"},{line,110}]},{ejabberd_router,route,3,
[{file,"src/ejabberd_router.erl"},{line,78}]},{ejabberd_c2s,check_privacy_route,5,
[{file,"src/ejabberd_c2s.erl"},{line,1886}]}]}

Inside of mod_offline_post.erl, I have this function:

offline_message(From, To, Packet) ->
Type = fxml:get_tag_attr_s(<<"type">>, Packet),
Body = fxml:get_path_s(Packet, [{elem, list_to_binary("body")}, cdata]),
Token = gen_mod:get_module_opt(To#jid.lserver, ?MODULE, auth_token, fun(S) -> iolist_to_binary(S) end, list_to_binary("")),
PostUrl = gen_mod:get_module_opt(To#jid.lserver, ?MODULE, post_url, fun(S) -> iolist_to_binary(S) end, list_to_binary("")),

if
    (Type == <<"chat">>) and (Body /= <<"">>) ->
        Sep = "&",
        Post = [
            "type=chat", Sep,
            "to=", To#jid.luser, Sep,
            "from=", From#jid.luser, Sep,
            "body=", binary_to_list(Body), Sep,
            "access_token=", Token
        ],
        ?INFO_MSG("Sending post request to ~s with body \"~s\"", [PostUrl, Post]),
        httpc:request(post, {binary_to_list(PostUrl), [], "application/x-www-form-urlencoded", list_to_binary(Post)},[],[]),
        ok;
    true ->
        ok
end.

It seemed like people were able to solve their issues by changing xml to fxml in later version, but it didn't look like it made a difference in my case.

If anyone has an idea on how I can solve this or any suggestions for a different way of achieving push notifications, please let me know. Thanks

回答1:

Since 16.12, Packet is now a #message{} record, so you should use xmpp library to work with it.

Here is how you should rewrite your code:

...
-include("xmpp.hrl").
...
offline_message(From, To, #message{type = Type, body = Body}) ->
    BodyTxt = xmpp:get_text(Body),
    ...
    if (Type == chat) and (BodyTxt /= <<"">>) ->
    ...

Consult xmpp.erl and xmpp_codec.hrl for details.