Git Product home page Git Product logo

apns4erl's Introduction

Apns4erl

This lib is intended to allow you to write an APNs provider for Apple Push Notificaion services (APNs) in Erlang.

Copyright (c) 2010 Inaka Labs SRL [email protected], released under the MIT license

Contact Us

For questions or general comments regarding the use of Apns4erl, please use our public hipchat room.

If you find any bugs or have a problem while using Apns4erl, please open an issue in this repo (or a pull request :)).

And you can check all of our open-source projects at inaka.github.io

Example

Using apns4erl is quite simple. First, setup something similar to this in your sys.config:

    {apns, [
      {apple_host, "gateway.sandbox.push.apple.com"},
      {apple_port, 2195},
      {cert_file, "/etc/certs/mycert_dev.pem"},
      {key_file, undefined},
      {cert_password, undefined},
      {timeout, 30000},
      {expires_conn, 300},
      {feedback_port, 2196},
      {feedback_host, "feedback.sandbox.push.apple.com"},
      {feedback_timeout, 18000000}
    ]}

NOTE: The apple_host to use will depend on your environment (production or development). Remember to always use the correct certificate, device tokens, and apns hostname for production or development environments.

NOTE 2: To generate the .pem file, from the .cer an .p12 files provided by Apple, you can use this script

Then, once you've started the apns application, you can connect to the APNS network using:

      apns:connect(
        %% your connection identifier:
        my_connection_name,
        %% called in case of a "hard" error:
        fun ?MODULE:handle_apns_error/2,
        %% called if the device uninstalled the application:
        fun ?MODULE:handle_apns_delete_subscription/1
      ).

As a result, you will get a tuple:

  • {ok, Pid}
  • {error, {already_started, Pid}}
  • {error, Reason}

Pid is the Pid of the apns_connection process spawned to handle the connection.

CAUTION: It is highly recommended to pass a fully qualified function for callbacks, using the fun M:F/A syntax. If you pass in a local fun using fun F/A, any subsequent code upgrades to the module where that local fun was defined, will cause the code server to kill processes that are holding on to references to the old code (eg. the apns connection processes, thus killing the apns4erl application).

To send a notification

apns:send_message(my_connection_name, "this_is_a_valid_device_token", "hello world").

That's it!

A little more about what's going on

Actually, send_message/3, send_message/4, send_message/5, send_message/6, send_message/7, and send_message/8 are calling send_message/2, which takes a #apns_msg record as its 2nd argument. Thus, you can also create the message customized with your own needs, by using a #apns_msg record:

    -include_lib("apns/include/apns.hrl").

    apns:send_message(my_connection_name, #apns_msg{
      alert  = "alert" ,
      badge  = 1,
      sound  = "sound" ,
      category = "EMAIL_ACTION",
      expiry = 1348000749,
      device_token = "this_is_a_valid_device_token"
    }).

Feedback Channel and Getting Errors

Notice how we are passing 2 funs to the connect function. These are used as callbacks:

If there was an error while sending a message, the first fun will be called.

If there were no errors, but Apple reported that the user removed the application from the device, the 2nd fun will be used (this is effectively the feedback channel).

    handle_apns_error(MsgId, Status) ->
      error_logger:error_msg("error: ~p - ~p~n", [MsgId, Status]).

    handle_apns_delete_subscription(Data) ->
      error_logger:info_msg("delete subscription: ~p~n", [Data]).

Passing Keys and Certificates Directly

By default, the private key and certificate to use to connect to Apple's servers are loaded out of the PEM-encoded files specified in cert_file and key_file. However, if you prefer to store these elsewhere and load them manually, you can pass DER-encoded binaries when connecting:

    CertBin = <<"-----BEGIN CERTIFICATE-----"...>>, % perhaps from a database
    KeyBin = <<"-----BEGIN RSA PRIVATE KEY-----"...>>,
    [{'Certificate', CertDER, not_encrypted}] = public_key:pem_decode(CertBin),
    [{'RSAPrivateKey', KeyDER, not_encrypted}] = public_key:pem_decode(KeyBin),
    Connection = #apns_connection{cert_file=undefined,
                                  cert=CertDER,
                                  key={'RSAPrivateKey', KeyDER}},
    apns:connect(Connection).

If you store your key and certificate as DER binaries, you can pass them directly without any decoding.

apns4erl's People

Contributors

elbrujohalcon avatar igaray avatar jebu avatar jahor avatar mdaguete avatar pnc avatar igorkarymov avatar rj avatar manuel-rubio avatar varnit avatar georgeye avatar chenyun90323 avatar comtihon avatar dergutemoritz avatar mhald avatar marcelog avatar essen avatar yjh0502 avatar jwheare avatar jczarniecki avatar bipthelin avatar spike886 avatar abs avatar alexdruzhilov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.