<commit_before>[![Build Status](https://secure.travis-ci.org/ileitch/rapns.png?branch=master)](http://travis-ci.org/ileitch/rapns)
[![Code Climate](https://codeclimate.com/github/ileitch/rapns.png)](https://codeclimate.com/github/ileitch/rapns)
[![Coverage Status](https://coveralls.io/repos/ileitch/rapns/badge.png?branch=master)](https://coveralls.io/r/ileitch/rapns?branch=master)
[![Gem Version](https://badge.fury.io/rb/rapns.png)](http://badge.fury.io/rb/rapns)

### Rapns - Professional grade APNs, GCM, ADM and WPNs for Ruby.

* Supports both APNs (iOS), GCM (Google Cloud Messaging, Android), ADM (Amazon Device Messaging) and WPNs (Windows Phone).
* Seamless Rails (3, 4) integration.
* Scalable - choose the number of persistent connections for each app.
* Designed for uptime - signal -HUP to add, update apps.
* Stable - reconnects database and network connections when lost.
* Run as a daemon or inside an [existing processs](https://github.com/ileitch/rapns/wiki/Embedding-API).
* Use in a scheduler for low-workload deployments ([Push API](https://github.com/ileitch/rapns/wiki/Push-API)).
* Reflection API for fine-grained instrumentation and error handling ([Reflection API](https://github.com/ileitch/rapns/wiki/Reflection-API)).
* Works with MRI, JRuby, Rubinius 1.9, 2.0, 2.1.
* Built with love.

### Who uses Rapns?

[GateGuru](http://gateguruapp.com) and [Desk.com](http://desk.com), among others!

*I'd love to hear if you use Rapns - @ileitch on twitter.*

## Getting Started

Add Rapns to your Gemfile:

    gem 'rapns'

Generate the migrations, rapns.yml and migrate:

    rails g rapns
    rake db:migrate

## Create an App & Notification

#### APNs

If this is your first time using the APNs, you will need to generate SSL certificates. See [Generating Certificates](https://github.com/ileitch/rapns/wiki/Generating-Certificates) for instructions.

```ruby
app = Rapns::Apns::App.new
app.name = "ios_app"
app.certificate = File.read("/path/to/sandbox.pem")
app.environment = "sandbox" # APNs environment.
app.password = "<PASSWORD>"
app.connections = 1
app.save!
```

```ruby
n = Rapns::Apns::Notification.new
n.app = Rapns::Apns::App.find_by_name("ios_app")
n.device_token = "..."
n.alert = "hi mom!"
n.attributes_for_device = {:foo => :bar}
n.save!
```

You should also implement the [ssl_certificate_will_expire](https://github.com/ileitch/rapns/wiki/Reflection-API) reflection to monitor when your certificate is due to expire.

#### GCM

```ruby
app = Rapns::Gcm::App.new
app.name = "android_app"
app.auth_key = "..."
app.connections = 1
app.save!
```

```ruby
n = Rapns::Gcm::Notification.new
n.app = Rapns::Gcm::App.find_by_name("android_app")
n.registration_ids = ["..."]
n.data = {:message => "hi mom!"}
n.save!
```

GCM also requires you to respond to [Canonical IDs](https://github.com/ileitch/rapns/wiki/Canonical-IDs).

#### ADM

```ruby
app = Rapns::Adm::App.new
app.name = "kindle_app"
app.client_id = "..."
app.client_secret = "..."
app.connections = 1
app.save!
```

```ruby
n = Rapns::Adm::Notification.new
n.app = Rapns::Adm::App.find_by_name("kindle_app")
n.registration_ids = ["..."]
n.data = {:message => "hi mom!"}
n.collapse_key = "Optional consolidationKey"
n.save!
```

For more documentation on [ADM](https://developer.amazon.com/sdk/adm.html).

## Starting Rapns

As a daemon:

    cd /path/to/rails/app
    rapns <Rails environment> [options]

Inside an existing process (see [Embedding API](https://github.com/ileitch/rapns/wiki/Embedding-API)):

```ruby
Rapns.embed<commit_msg>Add WPNS example in the readme<commit_after>[![Build Status](https://secure.travis-ci.org/ileitch/rapns.png?branch=master)](http://travis-ci.org/ileitch/rapns)
[![Code Climate](https://codeclimate.com/github/ileitch/rapns.png)](https://codeclimate.com/github/ileitch/rapns)
[![Coverage Status](https://coveralls.io/repos/ileitch/rapns/badge.png?branch=master)](https://coveralls.io/r/ileitch/rapns?branch=master)
[![Gem Version](https://badge.fury.io/rb/rapns.png)](http://badge.fury.io/rb/rapns)

### Rapns - Professional grade APNs, GCM, ADM and WPNs for Ruby.

* Supports both APNs (iOS), GCM (Google Cloud Messaging, Android), ADM (Amazon Device Messaging) and WPNs (Windows Phone).
* Seamless Rails (3, 4) integration.
* Scalable - choose the number of persistent connections for each app.
* Designed for uptime - signal -HUP to add, update apps.
* Stable - reconnects database and network connections when lost.
* Run as a daemon or inside an [existing processs](https://github.com/ileitch/rapns/wiki/Embedding-API).
* Use in a scheduler for low-workload deployments ([Push API](https://github.com/ileitch/rapns/wiki/Push-API)).
* Reflection API for fine-grained instrumentation and error handling ([Reflection API](https://github.com/ileitch/rapns/wiki/Reflection-API)).
* Works with MRI, JRuby, Rubinius 1.9, 2.0, 2.1.
* Built with love.

### Who uses Rapns?

[GateGuru](http://gateguruapp.com) and [Desk.com](http://desk.com), among others!

*I'd love to hear if you use Rapns - @ileitch on twitter.*

## Getting Started

Add Rapns to your Gemfile:

    gem 'rapns'

Generate the migrations, rapns.yml and migrate:

    rails g rapns
    rake db:migrate

## Create an App & Notification

#### APNs

If this is your first time using the APNs, you will need to generate SSL certificates. See [Generating Certificates](https://github.com/ileitch/rapns/wiki/Generating-Certificates) for instructions.

```ruby
app = Rapns::Apns::App.new
app.name = "ios_app"
app.certificate = File.read("/path/to/sandbox.pem")
app.environment = "sandbox" # APNs environment.
app.password = "<PASSWORD>"
app.connections = 1
app.save!
```

```ruby
n = Rapns::Apns::Notification.new
n.app = Rapns::Apns::App.find_by_name("ios_app")
n.device_token = "..."
n.alert = "hi mom!"
n.attributes_for_device = {:foo => :bar}
n.save!
```

You should also implement the [ssl_certificate_will_expire](https://github.com/ileitch/rapns/wiki/Reflection-API) reflection to monitor when your certificate is due to expire.

#### GCM

```ruby
app = Rapns::Gcm::App.new
app.name = "android_app"
app.auth_key = "..."
app.connections = 1
app.save!
```

```ruby
n = Rapns::Gcm::Notification.new
n.app = Rapns::Gcm::App.find_by_name("android_app")
n.registration_ids = ["..."]
n.data = {:message => "hi mom!"}
n.save!
```

GCM also requires you to respond to [Canonical IDs](https://github.com/ileitch/rapns/wiki/Canonical-IDs).

#### ADM

```ruby
app = Rapns::Adm::App.new
app.name = "kindle_app"
app.client_id = "..."
app.client_secret = "..."
app.connections = 1
app.save!
```

```ruby
n = Rapns::Adm::Notification.new
n.app = Rapns::Adm::App.find_by_name("kindle_app")
n.registration_ids = ["..."]
n.data = {:message => "hi mom!"}
n.collapse_key = "Optional consolidationKey"
n.save!
```

For more documentation on [ADM](https://developer.amazon.com/sdk/adm.html).

#### WPNS

```ruby
app = Rapns::Wpns::App.new
app.name = "windows_phone_app"
app.connections = 1
app.save!
```

```ruby
n = Rapns::Wpns::Notification.new
n.app = Rapns::Wpns::App.find_by_name("windows_phone_app")
n.uri = "http://..."
n.alert = "..."
n.save!
```

## Starting Rapns

As a daemon:

    cd /path/to/rails/app
    rapns <Rails environment> [options]

Inside an existing process (see [Embedding API](https://github.com/ileitch/rapns/wiki/Embedding-API)):

```ruby
Rapns.embed
{'fraction_non_alphanumeric': 0.16861571933641556, 'fraction_numerical': 0.0031275496328528694, 'mean_word_length': 4.563540090771558, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 28, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4565787', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2570, 'n_words': 614}