我试图让我的 ejabberd 服务器使用自定义的 offline_message_hook 模块发送离线推送通知。问题是钩子似乎永远不会被调用。我尝试将钩子的优先级设置为 0、49 和 50,但仍然不起作用。
这是模块的代码:
% name of module must match file name
-module(mod_offline_push).
%% Every ejabberd module implements the gen_mod behavior
%% The gen_mod behavior requires two functions: start/2 and stop/1
-behaviour(gen_mod).
%% public methods for this module
-export([start/2, stop/1, create_message/3]).
%% included for writing to ejabberd log file
-include("ejabberd.hrl").
%% ejabberd functions for JID manipulation called jlib.
-include("jlib.hrl").
start(_Host, _Opt) ->
?INFO_MSG("mod_offline_push loading", []),
inets:start(),
?INFO_MSG("HTTP client started", []),
ejabberd_hooks:add(offline_message_hook, _Host, ?MODULE, create_message, 0).
stop (_Host) ->
?INFO_MSG("stopping mod_offline_push", []),
ejabberd_hooks:delete(offline_message_hook, _Host, ?MODULE, create_message, 0).
create_message(_From, _To, Packet) ->
?INFO_MSG("creting offline message", []),
Type = xml:get_tag_attr_s("type", Packet),
FromS = xml:get_tag_attr_s("from", Packet),
ToS = xml:get_tag_attr_s("to", Packet),
if (Type == "chat") ->
post_offline_message(FromS, ToS)
end.
post_offline_message(From, To) ->
?INFO_MSG("Posting From ~p To ~p~n",[From, To]),
httpc:request(post, {"http://host.com/push.php",[],
"application/x-www-form-urlencoded",
lists:concat(["From=", From,"&To=", To])}, [], []),
?INFO_MSG("post request sent", []).