Ejabberd-推送通知APNS和GCM与mod_offline_push

时间:2017-02-07 17:35:34

标签: erlang ejabberd

我尝试使用ejabberd获取推送通知,我在此处找到了此链接:https://github.com/diamondbybold/ejabberd_mod_offline_post它应该在URL处发出针对脱机消息的POST请求。我编辑了我的ejabberd.yml文件以包含URL和令牌,但是,当我尝试在Ejabberd 16.12上运行它时,我遇到了一些问题。它 运行,但我发送消息给某人离线,我在/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}]}]}

在mod_offline_post.erl内部,我有这个功能:

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.

似乎人们可以通过在更高版本中将xml更改为fxml来解决他们的问题,但它看起来并没有像我的情况那样有所作为。

如果有人知道如何解决此问题或任何有关实现推送通知的方法的建议,请告知我们。感谢

1 个答案:

答案 0 :(得分:2)

自16.12以来,Packet现在是#message{}记录,因此您应该使用xmpp库来处理它。

以下是重写代码的方法:

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

详情请咨询xmpp.erlxmpp_codec.hrl