Erlang + Apple Push Notification [Error with invalid token]

I am currently trying to create a push messaging module for Erlang.

When the token is valid everything works fine ... The problem is that the old device token (which is not valid now) is rejected. I understand that an invalid token will be rejected by apns with a 6 byte socket message and will invalidate the connection (which is really stupid in my opinion, no matter ...)

The thing is, I am not getting the 6 byte socket message that APNS is supposed to provide me with my module, for example the control process is not listening on the socket.

Here's my code:


-export([start/1, init/1, handle_call/3, handle_cast/2, code_change/3, handle_info/2, terminate/2]).

-import(ssl, [connect/4]).
-record(push, {socket, pid, state, cert, key}).

start(Provisioning) ->
    gen_server:start_link(?MODULE, [Provisioning], []).

init([Provisioning]) ->
    gen_server:cast(self(), {connect, Provisioning}),
    {ok, #push{pid=self()}}.

send(Socket, DT, Payload) ->
    PayloadLen = length(Payload),
    DTLen = size(DT),
    PayloadBin = list_to_binary(Payload),
    Packet = <<0:8, 
    ssl:send(Socket, Packet).

handle_call(_, _, P) ->
    {noreply, P}.

handle_cast({connect, Provisioning}, P) ->
    case Provisioning of
    dev -> Address = "";
    prod -> Address = ""
    Port = 2195,
    Cert="/apns-" ++ atom_to_list(Provisioning) ++ "-cert.pem",
    Key="/apns-" ++ atom_to_list(Provisioning) ++ "-key.pem",
    Options = [{certfile, Cert}, {keyfile, Key}, {password, "********"}, {mode, binary}, {active, false}],
    Timeout = 1000,
    {ok, Socket} = ssl:connect(Address, Port, Options, Timeout),
    ssl:controlling_process(Socket, self()), %% Necessary ??
    gproc:reg({n,l, pushiphone}),
    {noreply, P#push{socket=Socket}};
handle_cast(_, P) ->
    {noreply, P}.

handle_info({ssl, Socket, Data}, P) ->
    <<Command, Status, SomeID:32/big>> = Data,
    io:fwrite("[PUSH][ERROR]: ~p / ~p / ~p~n", [Command, Status, SomeID]),
    {noreply, P};
handle_info({push, message, DT, Badge, [Message]}, P) ->
    Payload = "{\"aps\":{\"alert\":\"" ++ Message ++ "\",\"badge\":" ++ Badge ++ ",\"sound\":\"" ++ "msg.caf" ++ "\"}}",
    send(P#push.socket, DT, Payload),
    {noreply, P};
handle_info({ssl_closed, _SslSocket}, P) ->
    io:fwrite("SSL CLOSED !!!!!!~n"),
    {stop, normal, P};
handle_info(AnythingElse, P) ->
    io:fwrite("[ERROR][PUSH][ANYTHING ELSE] : ~p~n", [AnythingElse]),
    {noreply, P}.

code_change(_, P, _) ->
    {ok, P}.

terminate(_, _) ->


So when I run the module and I click on a valid token it is pushed to the phone, but when I click on an invalid token and then a valid token the valid token will not get clicked ...

I am aware that I have to listen to the loopback service to remove the device token from my database, but I also need to know if the push gateway has been stripped of my connection to reconnect.

So here's the real question : why isn't my genserver receiving an error packet (which should match handle_info ({ssl, Socket, Data}, P))?


source to share

1 answer

Your socket is configured with active = false. You won't get any messages unless you set it active = true (or re-active = once). See the documentation for inet: setopts / 2 .

You also don't need to set control_process to self ().



All Articles