2012-05-19 36 views
2

我目前正在測試一下我的推送通知模塊。 當設備令牌是無效的,它斷開......Erlang Apple推送通知在斷開連接之前沒有收到響應錯誤

根據Apple push notification developer documentation我應該只是蘋果推送服務器斷開連接之前得到一個錯誤響應包...

的事情是我得到斷開,但在此之前,我沒有在Socket上得到任何信息,並且我需要知道推送是否因推送格式不正確(所以我可以修復該錯誤)或無效的設備令牌(因此我可以將其從數據庫中移除) 。

這裏是我的代碼:

-module(pushiphone). 
-behaviour(gen_server). 

-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, state, cert, key}). 

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

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

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

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

handle_cast({connect, Provisioning}, P) -> 
    case Provisioning of 
    dev -> Address = "gateway.sandbox.push.apple.com"; 
    prod -> Address = "gateway.push.apple.com" 
    end, 
    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, true}], 
    Timeout = 1000, 
    {ok, Socket} = ssl:connect(Address, Port, Options, Timeout), 
    {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]), 
    ssl:close(Socket), 
    {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(_, _) -> 
    ok. 

當有效載荷和deviceToken都是對的它的偉大工程。如果deviceToken無效,它只會斷開連接。

有沒有人可以發現這個問題?因爲經過4個小時的搜索,我才發現我顯然不能!

這裏的錯誤響應表:

Status code  Description 
0   No errors encountered 
1   Processing error 
2   Missing device token 
3   Missing topic 
4   Missing payload 
5   Invalid token size 
6   Invalid topic size 
7   Invalid payload size 
8   Invalid token 
255   None (unknown) 
+0

您是否看到沙盒和/或生產網關的這種行爲? – johlo

+0

@johlo:其實都是...... – TheSquad

+0

如果ssl連接丟失怎麼辦? – why

回答

1

你似乎使用簡單的通知格式由圖5-1您在關聯(由send()判斷蘋果文檔中定義功能)。當使用這種格式時,當請求格式錯誤時不會提供錯誤響應 - 您只需斷開連接。

要獲得錯誤響應,您應該使用圖5-2中詳述的增強型通知格式

+0

當然!我怎麼錯過了! – TheSquad