使用erlang发送apns

时间:2017-07-21 18:14:23

标签: ssl erlang apple-push-notifications ejabberd

这是我用来发送apns的代码的一部分。

  Options = case Password of
      undefined ->
          [{certfile, Cert}, {keyfile, Keyfile}, {mode, binary}];
      _ ->
        [{certfile, Cert}, {keyfile, Keyfile}, {password, Password}, {mode, binary}]
  end,
  case ssl:connect(Address, Port, Options, ?Timeout) of
        {ok, Socket} ->
            PayloadBin = list_to_binary(Payload),
            PayloadLength = size(PayloadBin),
            TokenNum = erlang:binary_to_integer(Token, 16),
            TokenBin = <<TokenNum:32/integer-unit:8>>,
            Packet = <<
                0:8,
                32:16/big,
                TokenBin/binary,
                PayloadLength:16/big,
                PayloadBin/binary
            >>,
            ssl:send(Socket, Packet),
            ssl:close(Socket),
            ?DEBUG("mod_apns: Successfully sent payload to the APNS server", []),
            ok;
        {error, Reason} ->
            ?ERROR_MSG("mod_apns: Unable to connect:~p to the APNS server ~p: ~p", [Options, Address, Reason]),
            Reason
    end

但是我收到了这个错误:

Unable to connect:[{certfile,<<"/etc/certificates/myCer.pem">>},{keyfile,<<"/etc/certificates/myKey.pem">>},{mode,binary}] to the APNS server <<"gateway.push.apple.com">>: {options,{socket_options,[{mode,binary}]}}

这是一个ejabberd模块,相同的代码适用于ejabberd 17.04但不适用于17.06。
证书和密钥是有效的,正如我所说,我可以使用与旧版ejabberd相同的erlang模块获得apns。
我是erlang的新手,我不理解错误消息({options,{socket_options,[{mode,binary}]}})

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

问题是我发送的是二进制而不是列表:

mod_opt_type(address) -> fun binary_to_list/1;
mod_opt_type(port) -> fun(I) when is_integer(I) -> I end;
mod_opt_type(certfile) -> fun binary_to_list/1;
mod_opt_type(keyfile) -> fun binary_to_list/1;