FCM integration with ejabberd

Integrating FCM with ejabberd in order to send push notifications.

On the client side I have setup the FCM. On ejabberd's side I am unable to integrate it.

My first step is to pass the FCM token to the ejabberd so that the token is sent to the XMPP to check whether the user is online or offline and then send a push notification.

My ejabberd setup goes as follows: Server - Ubuntu 20.04 ejabberd version - 23.0.1 erlang version - 22.0.1

My setup looks like a pre built version of the ejabberd. It is installed in the /opt and there are two folders ejabberd/ and ejabberd-23.01/ .The directory ejabberd/ has the configuration file and the certificates for my domain. The ejabberd-23.01/ has these directories bin/ erts-12.3.2.5/ lib/ releases/ The lib/ has the ejabberd-23.0.1/ directory which has the ebin/ directory as well with the .beam files for all the modules. I am not sure whether the modules are running from this location or not.

So this is what I tried:

  1. I tried to integrate FCM through the mod_push module using fcm_api_key but found out that it is supported.
  2. Build a custom module: - I cloned the ejabberd repository - I downloaded the erl shell(v22.0.1) and rebar3. - I wrote this erlang module -
-module(mod_fcm_rest).
-behaviour(gen_mod).

-export([start/2, stop/1, handle_rest_request/1]).

-include("ejabberd.hrl").
-define(INFO_MSG(Level, Msg), io:format("~p: ~s~n", [Level, Msg])).


%% Called when the module starts
start(_Host, _Opts) ->
    ok.

%% Called when the module stops
stop(_Host) ->
    ok.

%% Handle REST API requests
handle_rest_request(Req) ->
    %% Extract information from the request
    {<<"fcm_token">>, Token} = cowboy_req:body(Req),

    %% Log the token for debugging
    ?INFO_MSG("Received FCM token: ~p", [Token]),

    %% Store the token in Mnesia or Redis (implementation needed)
    %% For example:
    %% mnesia:write({fcm_token, UserJID, Token}),

    %% Send a response back
    {ok, Resp} = cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, <<"{\"status\": \"success\"}">>, Req),
    Resp.

FYI - This is a sample module that I got on a site.

I built the module through rebar3 and got few errors which I fixed with the help of google. I got the .beam file for my custom module from the _build/ folder and copied it to this location /opt/ejabberd-23.01/lib/ejabberd-23.01/ebin. I called the module in the ejabberd.yml as follows modules: mod_fcm_rest:{}

ejabberd service fails with this configuration and gives an error saying mod_fcm_rest is not found. 3. I tried to write an external script that can fetch the fcm token from the frontend and send it to the ejabberd as a iq stanza. But that script was not being recognized in the ejabberd configuration and ejabebrd service used to fail when I mentioned the script in the yml file.

Can anyone give me a solution to integrate my ejabberd with the FCM to send push notifications?

Upvotes: 0

Views: 21

Answers (0)

Related Questions