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
代码下载地址 http://pan.baidu.com/s/14GIEa

转载于:https://www.cnblogs.com/yanllearnn/p/3796664.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值