apns推送

 

 

 

 

 

%%%-------------------------------------------------------------------                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
%%% @doc
%%%
%%% @end
%%% Created : 31 Aug 2012 [email protected] 
%%%-------------------------------------------------------------------

-module(test_apns).
-record(apns_connection, {apple_host        = "gateway.sandbox.push.apple.com"      :: string(),
                          apple_port        = 2195                                  :: integer(),
                          cert_file         = "priv/cert.pem"                       :: string(),
                          key_file          = undefined                             :: undefined | string(),
                          timeout           = 30000                                 :: integer(),
                          error_fun         = fun(X,Y) -> erlang:display({X,Y}) end :: fun((binary(), apns:status()) -> stop | _),
                          feedback_host     = "feedback.sandbox.push.apple.com"     :: string(),
                          feedback_port     = 2196                                  :: integer(),
                          feedback_fun      = fun erlang:display/1                  :: fun((string()) -> _),
                          feedback_timeout  = 30*60*1000                            :: pos_integer()
                          }).
-record(apns_msg, {id = apns:message_id()       :: binary(),
                   expiry = apns:expiry(86400)  :: non_neg_integer(), %% default = 1 day
                   device_token                 :: string(),
                   alert = none                 :: none | apns:alert(),
                   badge = none                 :: none | integer(),
                   sound = none                 :: none | string(),
                   extra = []                   :: [apns_mochijson2:json_property()]}).
-record(loc_alert, {body    = none  :: none | string(),
                    action  = none  :: none | string(),
                    key     = ""    :: string(),
                    args    = []    :: [string()],
                    image   = none  :: none | string()}).


         push_token/1]).
get_pid() ->
    {ok, Pid} = apns:connect(),
    Pid.
delete_pid(ConnId) ->
    apns:disconnect(ConnId).

push_by_pid(ConnId, Token1) ->
     Alert1 = #apns_msg{device_token = Token1,
                       badge = 9,
                       sound = "bingbong.aiff",
                       %extra = [{"jump","yes"}],
                       extra = [{jump, yes}],
                       alert = #loc_alert{body="hello1"
                                        }},
    io:format("In ~p:appns_packet ~p Alert1 = ~p ~n", [?MODULE, ?LINE, Alert1]),
    apns:send_message(ConnId, Alert1).


-spec push(DeviceToken :: string(), Alert :: tuple()) -> ok.
push(DeviceToken, Alert)->
    Conn = apns:connect(),
    case Conn of
        {ok,ConnId} ->
            Res = apns:send_message(ConnId, DeviceToken, Alert),
            Res1 = apns:send_message(ConnId, DeviceToken, Alert),
            case Res of
                ok ->
                    ok;
                Err ->
                    Err
            end,
            apns:disconnect(ConnId);
        Err ->
            Err
    end.
  1 #!/bin/sh
  2 
  3 #Usage:
  4 #test_certs {cert_file} {private_key_file}
  5 #Example:
  6 #test_certs aps_developer_indetity.cer aps_developer_identity.p12
  7 mkdir -p ./temp                                                             
  8 openssl pkcs12 -in "$2" -out ./temp/key-enc.pem
  9 openssl rsa -in ./temp/key-enc.pem -out ./temp/key.pem
 10 openssl x509 -inform der -in "$1" -out ./temp/cert.pem
 11 cat ./temp/cert.pem ./temp/key.pem > ./cert.pem
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                              

你可能感兴趣的:(apns)