erlang -- ios apns provider -- erlang 实现

os apns-apple notification server 与第三方provider的通信原理网上已有很多介绍,这里不再介绍,有想了解的大家可以去IOS官网https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW1
学习了解.
网上关于IOS apns自己实现的很少,一般是Java或者PHP,目前还有一个用C++实现的,看了看,然后自己想用erlang实现与IOS apns的通信,进过努力实现了,与大家分享.
-define(URL,"gateway.sandbox.push.apple.com").
-define(PORT,2195).
-define(DEVICE_TOKEN,"3bd9dbf509cd92d41a3334d8ab0fcced1a6fc27c7c1bc60324b2bafe1c15cadf").
start() ->
ssl:start(),
case gen_tcp:connect(?URL,?PORT,[binary],infinity) of
{ok,Socket} ->
case ssl:connect(Socket,[{certfile,"./ios_app/PushChatCert.pem"},
{keyfile,"./ios_app/PushChatKey.pem"},
{password,"123456"}],
infinity) of
{ok,SslSocket} ->
io:format("####socket connect:~p~n",["ok"]),
Command = <<0:8>>,
Token = ?DEVICE_TOKEN,
DeviceList = change_16_to_10(Token,[]),
Device = list_to_binary(DeviceList),
io:format("##Device:~p~n",[Device]),
DeviceL = length(DeviceList),
DeviceBin = <<DeviceL:16>>,
Message = "{\"aps\":{\"alert\":\"哈哈我的测试\",\"badge\":4,\"sound\":\"default\"}}",
MessageL = length(Message),
MessageBin = <<MessageL:16>>,
Data = list_to_binary([Command,DeviceBin,Device,MessageBin,Message]),
case ssl:send(SslSocket,Data) of
ok ->
io:format("######send ok!~n");
{error,SReason} ->
io:format("####SReason:send error:~p~n",[SReason])
end;
{error,Reason} ->
io:format("####Reason:~p~n",[Reason])
end;
{error,FRe} ->
io:format("#FRe:~p~n",[FRe])
end.
 
 
change_16_to_10([],L) ->
lists:reverse(L);
change_16_to_10([H,T|Tail],L) ->
First = assci_to_10_type(H),
Second = assci_to_10_type(T),
One = First * 16 + Second,
change_16_to_10(Tail,[One|L]).
 
 
assci_to_10_type(Term) when Term >= $a andalso Term =< $f ->
Term - $a + 10;
assci_to_10_type(Term) when Term >= $A,Term =< $F ->
Term - $A + 10;
assci_to_10_type(Term) when Term >= $0,Term =< $9 ->
Term - $0;
assci_to_10_type(_Term) ->
0.
 
与苹果apns通信的证书怎么来的?参考IOS官方文档.
简单介绍:需要iOS开发者提供给服务端的开发者aps_production.cer从苹果开发者网站下载;
然后在终端中输入下面命令
openssl x509 -in aps_production.cer -inform der -out PushChatCert.pem
得到PushChatCert.pem
双击安装aps_production.cer到钥匙串,在钥匙串中找到刚才安装的推送证书,点开,右键导出秘要,命名为youkey.p12;demo中是PushChatKey.p12
然后在终端中输入下面命令
openssl pkcs12 -nocerts -out PushChatKey.pem -in  PushChatKey.p12
此处需要输入密码,demo中的密码:123456
重复确认密码,得到PushChatKey.pem
posted @ 2014-06-19 15:18  learnn  阅读(544)  评论(0编辑  收藏  举报