ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - chvanikoff/apns4ex: APNS for Elixir
APNS for Elixir. Contribute to chvanikoff/apns4ex development by creating an account on GitHub.
Visit Site

GitHub - chvanikoff/apns4ex: APNS for Elixir

GitHub - chvanikoff/apns4ex: APNS for Elixir

APNS

The library was inspired by Apns4erl

Warning

The older version of the library is available in 0.0.x-stable branch

Installation

  1. Add apns to your list of dependencies in mix.exs:

    def deps do [{:apns, "~> 0.9.6"}] end

  2. Ensure apns is started before your application:

    def application do [applications: [:apns]] end

Usage

Config the APNS app and define pools

config :apns,
  # Here goes "global" config applied as default to all pools started if not overwritten by pool-specific value
  callback_module:    APNS.Callback,
  timeout:            30,
  feedback_interval:  1200,
  support_old_ios:    true,
  expiry:    60,
  # Here are pools configs. Any value from "global" config can be overwritten in any single pool config
  pools: [
    # app1_dev_pool is the pool_name
    app1_dev_pool: [
      env: :dev,
      pool_size: 10,
      pool_max_overflow: 0,
      certfile: "/path/to/cert.pem",
      keyfile: "/path/to/key.pem"
    ],
    app1_prod_pool: [
      env: :prod,
      certfile: "/path/to/cert.pem",
      keyfile: "/path/to/key.pem",
      pool_size: 100,
      pool_max_overflow: 0
    ],
  ]

Config keys

Name Default value Description
cert nil Plaintext APNS certfile content (not needed if certfile is specified)
certfile nil Path to APNS certificate file or a tuple like {:my_app, "certs/cert.pem"}
which will use a path relative to the priv folder of the given application (not needed if cert is specified)
cert_password nil APNS certificate password (if any)
key nil Plaintext APNS keyfile content (not needed if keyfile is specified)
keyfile nil Path to APNS keyfile (not needed if key is specified)
callback_module APNS.Callback This module will receive all error and feedback messages from APNS
timeout 30 Connection timeout in seconds
feedback_interval 1200 The app will check Apple feedback server every feedback_interval seconds
support_old_ios true Push notifications are limited by 256 bytes (2kb if false), this option can be changed per message individually
expiry 60 Seconds Apple will re-try to deliver the push notification*
pools [] List of pools to start

* It should be noted that Apple will always try to deliver the message at least once. If it takes longer to send the message to Apple than the expiry offset it will still be delivered.

Pool keys

Pool key Description
env :dev for Apple sandbox push server or :prod for Apple production push server
pool_size Maximum pool size
pool_max_overflow Maximum number of workers created if pool is empty*

* WARNING: According to our tests a overflow other then zero is bad.

All pools defined in config will be started automatically

From here and now you can start pushing your PNs via APNS.push/2 and APNS.push/3:

message = APNS.Message.new
message = message
|> Map.put(:token, "0000000000000000000000000000000000000000000000000000000000000000")
|> Map.put(:alert, "Hello world!")
|> Map.put(:badge, 42)
|> Map.put(:extra, %{
  "var1" => "val1",
  "var2" => "val2"
})
APNS.push :app1_dev_pool, message

or

APNS.push :app1_prod_pool, "0000000000000000000000000000000000000000000000000000000000000000", "Hello world!"

Handling APNS errors and feedback

You can define a callback handler module via config param callback_module. The module should implement 2 functions:

  • error/2 which receives %APNS.Error{} and token
  • feedback/1 which receives %APNS.Feedback{}
  defmodule APNS.Callback do
    def error(error = %APNS.Error{}, token) do
      # handle error
    end

    def feedback(feedback = %APNS.Feedback{}) do
      # handle feedback
    end
  end

Structs

  • %APNS.Message{}
defstruct [
  id: nil,
  expiry: 60,
  token: "",
  content_available: nil,
  alert: "",
  badge: nil,
  sound: "default",
  mutable_content: nil,
  priority: 10,
  extra: [],
  support_old_ios: nil
]
  • %APNS.Error{}
defstruct [
  message_id: nil,
  status: nil,
  error: nil
]
  • %APNS.Feedback{}
defstruct [
  time: nil,
  token: nil
]
  • %APNS.Message.Loc{}
defstruct [
  title: "",
  body: "",
  title_loc_key: nil,
  title_loc_args: nil,
  action_loc_key: nil,
  loc_key: "",
  loc_args: [],
  launch_image: nil
]

Contribute

git clone [email protected]:chvanikoff/apns4ex.git
# update config/config.exs with the path to your cert (do not use your live cert)
mix test

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory