如何使用Erlang发送推送通知? [英] How to send a push notification using Erlang?

查看:230
本文介绍了如何使用Erlang发送推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Erlang向APN发送推送通知。
这是迄今为止我提出的代码:

I'm trying to send a push notification to APNs using Erlang. This is the code I came up with so far:

-module(apnstest2).
-export([connect/0]).

connect() ->
    application:start(ssl),
    ssl:seed("someseedstring"),
    Address = "gateway.sandbox.push.apple.com",
    Port = 2195,
    Cert = "/path/to/Certificate.pem",
    Key = "/path/to/Key.unenc.pem",
    Options = [{certfile, Cert}, {keyfile, Key}, {mode, binary}],
    Timeout = 1000,
    {ok, Socket} = ssl:connect(Address, Port, Options, Timeout),

    Token = "195ec05a962b24954693c0b638b6216579a0d1d74b3e1c6f534c6f8fd0d50d03",
    Payload = "{\"aps\":{\"alert\":\"Just testing.\",\"sound\":\"chime\", \"badge\":10}}",
    TokenLength = length(Token),
    PayloadLength = length(Payload),

    Packet = [<<0:8, TokenLength, Token, PayloadLength, Payload>>],

    ssl:send(Socket, list_to_binary(Packet)),
    ssl:close(Socket).

代码不利用Erlang的并发功能,只是一个原型。我只想测试是否可以以最简单的方式发送推送。

The code doesn't take advantage of Erlang's concurrency but is just a prototype. I only want to test if I can send the push in the most simple way.

我认为发送到APN的数据包中存在问题。
这是推送通知的二进制格式:

I think the problem is in the packet being sent to the APNs. This is the binary format of a push notification:

替代文本http://developer.apple.com/IPhone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Art/aps_provider_binary.jpg

如何在Erlang中创建这样的数据包?
有人可以看看我的代码,告诉我问题在哪里?

我也用Erlang的SSL应用程序创建连接并发送数据,我不知道这是问题或数据包。

谢谢!

How should I create such a packet in Erlang? Could someone please take a look at my code and tell me where the problem is?
Also I used Erlang's SSL application to create the connection and send the data and I don't know if this is the problem or the packet.
Thanks!

推荐答案

首先,没有需要创建单个二进制文件的列表,然后调用 list_to_binary / 1 。您可以发送二进制文件。

To start with, there is no need for creating a list of a single binary and then calling list_to_binary/1 on it. You can just send the binary itself.

另外,请确保根据协议,字段长度和值是否合适:

Also, make sure the field lengths and values are appropriate according to the protocol:

TokenLength = 32 = length(Token),
Packet = <<0:8, TokenLength:16/big, Token, PayloadLength:16/big, Payload>>,
ssl:send(Socket, Packet),

现在我们我们会看到这个长度(Token)实际上是64,而不是32:
你忘记将令牌的十六进制字符串转换为二进制,所以你发送一个64字节的十六进制字符串而不是32个二进制字节。

Now that we have gotten this far, we will see that length(Token) is in fact 64, not 32: You forgot to convert the hex string for Token to a binary, so you are sending a 64 byte hex character string instead of 32 binary bytes.

所以...从一开始就做一个二进制的负载,令令牌成为一个数字常量,你可以做一些类似下面的事情:

So... making Payload a binary from the start, and making Token a numeric constant, you can do something like the following:

Payload = <<"{\"aps\":{\"alert\":\"Just testing.\",\"sound\":\"chime\", \"badge\":10}}">>,
PayloadLength = size(Payload),
Packet = <<0:8, 32:16/big,
          16#195ec05a962b24954693c0b638b6216579a0d1d74b3e1c6f534c6f8fd0d50d03:256/big,
          PayloadLength:16/big, Payload/binary>>,
ssl:send(Socket, Packet),

感谢 Christian 指出在这个答案的前修订中有一些错误。

Thanks to Christian for pointing out a number of mistakes in the former revisions of this answer.

这篇关于如何使用Erlang发送推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆