首页 > 解决方案 > 如何在 ejabberd 上使用 FCM 连接和发送推送通知?

问题描述

我正在尝试使用 FCM 向 android 客户端发送推送通知。我点击以下链接:https ://github.com/vkatsuba/epns但收到以下错误:异常错误:未定义函数 epns:push/2这是我的代码:```

-behaviour(gen_mod).
-export([start/2, stop/1, mod_options/1, mod_doc/0, depends/2, create_message/1, post_offline_message/3]).
-include("/home/faiqkhan/ejabberd-20.07/lib/fast_xml-1.1.43/include/fxml.hrl").
-include("/home/faiqkhan/ejabberd-20.07/lib/ejabberd-20.07/include/logger.hrl").
-include("/home/faiqkhan/ejabberd-20.07/lib/ejabberd-20.07/include/translate.hrl").
start(_Host, _Opt) -> 
    ?INFO_MSG("mod_http_offline loading", []),
    ejabberd_hooks:add(offline_message_hook, _Host, ?MODULE, create_message, 0).
stop (_Host) -> 
    ?INFO_MSG("stopping mod_http_offline", []),
    ejabberd_hooks:delete(offline_message_hook, _Host, ?MODULE, create_message, 0).
create_message({_Action, Packet} = Acc) ->
    % ?INFO_MSG("Packet details: ~p", [Packet]),
    EncodedData = xmpp:encode(Packet),
    % ?INFO_MSG("Encoded Packet: ~p", [EncodedData]),
    {value, To} = fxml:get_tag_attr(<<"to">>, EncodedData),
    {value, From} = fxml:get_tag_attr(<<"from">>, EncodedData),
    Body = fxml:get_path_s(EncodedData, [{elem, <<"body">>}, cdata]),
    To_id = lists:nth(1,string:lexemes(To,"@")),
    From_id = lists:nth(1,string:lexemes(From,"@")),
    ?INFO_MSG("To id: ~p",[To_id]),
    ?INFO_MSG("From id: ~p",[From_id]),
    ?INFO_MSG("PacketsBody ~p",[Body]),
    post_offline_message(From_id, To_id, Body),
    Acc.
post_offline_message(From, To, Body) ->
    ?INFO_MSG("Posting From ~p To ~p Body ~p~n",[From, To, Body]),
    ?INFO_MSG("post request sent (not really yet)", []),
    FCMData = #{
      key => "fcm-key",
      url => "https://fcm.googleapis.com/fcm/send",
      playload => #{
        to => <<"rid">>,
        priority => <<"high">>,
           data => #{
             <<"title">> => <<"Some Title">>,
             <<"some_custom_field">> => true
            }
        }
    }, epns:push(fcm, FCMData).

标签: firebase-cloud-messagingerlangejabberd

解决方案


exception error: undefined function epns:push/2

This means that Erlang couldn't find the file epns.beam with the function push/2 defined.

From what I see, that module defines and exports that function correctly, https://github.com/vkatsuba/epns/blob/master/src/epns.erl

So probably the problem is that you forgot some step:

  1. compile that epns library with the same Erlang version that you run ejabberd
  2. copy the *.beam files to a path where erlang can find them (for example, copy those files with all the other ejabberd *.beam files)

推荐阅读