ejabberd mod_offline iPhone推送通知
我目前正在开发一个聊天 iPhone 应用程序,到目前为止,对于 ejabberd 服务器的常规配置来说效果很好。我想在用户“离线”时实现推送通知,为此我只需要运行一个 PHP 脚本,该脚本获取令牌设备和文本消息以通过 SSL 传递到 Apple 的服务器(Apple 部分已完成),我的问题开始是我不知道如何在我的 ejabberd 服务器上实施此操作?基本上我只需要在可能的情况下对收到的离线消息创建一个操作。有人能指出我正确的方向吗?我已经设法开始为新模块编写一些代码,但是当离线消息调用该模块时,我总是遇到相同的错误,这是代码和错误。
module.erl
-module(mod_offline_push).
-behaviour(gen_mod).
-include("ejabberd.hrl").
-export([start/2, stop/1, send_notice/1]).
start(VHost,_Opts) ->
?INFO_MSG("Starting mod_offline_push Host: ~p", [VHost]),
inets:start(),
ssl:start(),
ejabberd_hooks:add(offline_message_hook, VHost, ?MODULE,send_notice, 50),
ok.
stop(VHost) ->
?INFO_MSG("mod_offline_push stopping Host: ~p", [VHost]),
ejabberd_hooks:delete(offline_message_hook, VHost, ?MODULE, send_notice, 50),
ok.
send_notice(Packet) ->
?INFO_MSG("after http:",[]),
Type = xml:get_tag_attr_s("type", Packet),
FromS = xml:get_tag_attr_s("from", Packet),
ToS = xml:get_tag_attr_s("to", Packet),
Body = xml:get_path_s(Packet, [{elem, "body"}, cdata]),
if
(Type == "chat") and (Body /= "") ->
Sep = "&",
Post = [
"application=",ToS, Sep,
"event=", FromS,Type, Sep,
"description=", Body, Sep,
"priority=-1" ],
httpc:request(post, {"http://pushNotification/push", [], "application/x-www-form-urlencoded", list_to_binary(Post)},[],[]),
ok;
true ->
ok
end.
错误
=ERROR REPORT==== 2010-08-26 16:53:19 ===
E(<0.370.0>:ejabberd_hooks:190) : {undef,
[{mod_offline_push,send_notice,
[{jid,"userA","198.165.211.1",
"2121731711282852044419503",
"userA","198.165.211.206",
"2121731711282852044419503"},
{jid,"userB","198.165.211.1",
[],"userB","198.165.211.1",[]},
{xmlelement,"message",
[{"type","chat"},
{"to","[email protected]"}],
[{xmlelement,"body",[],
[{xmlcdata,<<"Hello">>}]}]}]},
{ejabberd_hooks,run1,3},
{ejabberd_sm,route,3},
{ejabberd_local,route,3},
{ejabberd_router,route,3},
{ejabberd_c2s,session_established,2},
{p1_fsm,handle_msg,10},
{proc_lib,init_p,5}]}
running hook: {offline_message_hook,
[{jid,"userA","198.165.211.1",
"2121731711282852044419503","userA",
"userA","2121731711282852044419503"},
{jid,"userB","198.165.211.1",[],"userB",
"198.165.211.1",[]},
{xmlelement,"message",
[{"type","chat"},{"to","[email protected]"}],
[{xmlelement,"body",[],[{xmlcdata,<<"Hello">>}]}]}]}
I'm currently developing a chat iPhone app and so far so good for regular configuration for the ejabberd server. I want to implement Push notification when the user is "offline" and to do this I just need to run a PHP script which gets a Token device and the text message to deliver via SSL to Apple's servers (the Apple part is done), my problem begins is that i have no clue how to implement this action to my ejabberd server? basically I just need to create an action on a received offline message i this possible. Can someone point me in the right direction. I have manage to begin writing some code for a new module but I get same error all the time when this module is called by the offline messages here's the code and the error.
module.erl
-module(mod_offline_push).
-behaviour(gen_mod).
-include("ejabberd.hrl").
-export([start/2, stop/1, send_notice/1]).
start(VHost,_Opts) ->
?INFO_MSG("Starting mod_offline_push Host: ~p", [VHost]),
inets:start(),
ssl:start(),
ejabberd_hooks:add(offline_message_hook, VHost, ?MODULE,send_notice, 50),
ok.
stop(VHost) ->
?INFO_MSG("mod_offline_push stopping Host: ~p", [VHost]),
ejabberd_hooks:delete(offline_message_hook, VHost, ?MODULE, send_notice, 50),
ok.
send_notice(Packet) ->
?INFO_MSG("after http:",[]),
Type = xml:get_tag_attr_s("type", Packet),
FromS = xml:get_tag_attr_s("from", Packet),
ToS = xml:get_tag_attr_s("to", Packet),
Body = xml:get_path_s(Packet, [{elem, "body"}, cdata]),
if
(Type == "chat") and (Body /= "") ->
Sep = "&",
Post = [
"application=",ToS, Sep,
"event=", FromS,Type, Sep,
"description=", Body, Sep,
"priority=-1" ],
httpc:request(post, {"http://pushNotification/push", [], "application/x-www-form-urlencoded", list_to_binary(Post)},[],[]),
ok;
true ->
ok
end.
ERROR
=ERROR REPORT==== 2010-08-26 16:53:19 ===
E(<0.370.0>:ejabberd_hooks:190) : {undef,
[{mod_offline_push,send_notice,
[{jid,"userA","198.165.211.1",
"2121731711282852044419503",
"userA","198.165.211.206",
"2121731711282852044419503"},
{jid,"userB","198.165.211.1",
[],"userB","198.165.211.1",[]},
{xmlelement,"message",
[{"type","chat"},
{"to","[email protected]"}],
[{xmlelement,"body",[],
[{xmlcdata,<<"Hello">>}]}]}]},
{ejabberd_hooks,run1,3},
{ejabberd_sm,route,3},
{ejabberd_local,route,3},
{ejabberd_router,route,3},
{ejabberd_c2s,session_established,2},
{p1_fsm,handle_msg,10},
{proc_lib,init_p,5}]}
running hook: {offline_message_hook,
[{jid,"userA","198.165.211.1",
"2121731711282852044419503","userA",
"userA","2121731711282852044419503"},
{jid,"userB","198.165.211.1",[],"userB",
"198.165.211.1",[]},
{xmlelement,"message",
[{"type","chat"},{"to","[email protected]"}],
[{xmlelement,"body",[],[{xmlcdata,<<"Hello">>}]}]}]}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
堆栈跟踪表明,
mod_offline_push
中没有接受三个 参数(两个 JID 和数据包本身)的send_notice
函数。您的函数的签名不匹配,因为它只需要 1 个参数。该钩子需要三参数回调,所以尝试使用
what the stacktrace says, is that there is no
send_notice
function in themod_offline_push
that accepts three parameters (the two JIDs ones and the packet itself). The signature of your function doesn't match, as it expect only 1 argument.The hook is expecting three-argument callbacks, so try with
请使用下面的博客,内容非常丰富。他们的代码对我来说效果很好。
http://symmetryinfinity.com/2013/01/23 /ios-push-notifications-from-ejabberd.html
Please use the below blog , very informative.Their code works fine for me.
http://symmetricinfinity.com/2013/01/23/ios-push-notifications-from-ejabberd.html
确保删除源文件中的 INFO_MSG 行。之后重新编译并替换 ejabberd 文件夹中的 Beam 文件。不要忘记重新启动 ejabberd。
Make sure you remove the INFO_MSG lines in the source files.After that recompile and replace the beam files in the ejabberd folder.Don't forget to restart ejabberd.
我使用了 mod_interact,它在离线时调用 api。我已经设置了 api 来发送推送通知。
Ive used mod_interact which when offline calls an api. I have setup the api to send a push notification.