ActsAsPushable

Build Status Gem Version

A gem for Ruby on Rails that makes managing devices and push notifications for both iOS and Android easy and reliable.

Setup

Add ActsAsPushable to your Gemfile

  gem 'acts_as_pushable'
  bundle install

or just install it -

  gem install acts_as_pushable

Quick start

Run the generator to create the migrations and initializer template -

  rails generate act_as_pushable:install
  rake db:migrate

This a file at config/initializers/acts_as_pushable.rb that you can modify to match your settings. Follow the quick start guides for the platforms that you want to support.

Next add acts_as_pushable the following to the model that you want to have devices, this is usually the User model. It should look something like this -

  class User < ActiveRecord::Base
    acts_as_pushable
  end

Quick start - iOS

Before we start you'll need a push certificate for both APN Development and APN Production. You can find instructions on how to do this here.

Important: You should do both production and development and include them in all deployment environments. These relate to the two environments that Apple provide rather than your environments.

Once you have the certificate from Apple, you will need to export your key and the installed certificate as p12 files. Here how you do this -

  1. In Keychain Access find your installed certificate
  2. Right click the certificate, it should have an arrow next to it if install correctly, then export it. export
  3. Next convert the p12 file to a pem file by running the following command. $ openssl pkcs12 -in cert.p12 -out apple_push_notification.pem -nodes -clcerts
  4. The output pem file should be put in one of the following locations depending on it's type -
    • config/acts_as_pushable/apn/production.pem
    • config/acts_as_pushable/apn/development.pem

Note: You can change the location that the pem file is loaded stored in the acts_as_pushable.rb initializer.

Quick start - Android

  1. Register for GCM (Google Cloud Messaging) at developers.google.com.
  2. Enter your key into the acts_as_pushable.rb initializer. You can get this from the Google API Developer Console. It should be entered as such -
  ActsAsPushable.configure do |config|
    config.gcm_key = 'replace_me_with_your_api_key'
  end

Add a device

You can add a device to any model with acts_as_pushable. In these examples we'll use a model named User as an example.

  user = User.create
  user.add_device({
    token: 'the_token_generated_by_the_device',
    platform: "ios",
    platform_version: "9.3",
    push_environment: "development",
  })

Send a push notification to a user

Sending a push notification to a user will send the message to all of their valid device tokens both on iOS & Android

  user = User.find(id)
  user.send_push_notification(title: 'My App', message: 'this is a test', options)

You might want to consider doing this inside a worker.

Send a push notification to a specific device

  user = User.find(id)
  device = user.devices.first

  case device.platform
  when "ios"
    # iOS does not support titles
    device.send_push_notification(message: 'this is a test', options)
  when "android"
    device.send_push_notification(message: 'this is a test', { title: 'My App' })
  end

You might want to consider doing this inside a worker.

Sending silent push notifications to iOS Devices

You can send silent push notifications with no message to iOS devices. This is useful when you just want to update the badge count on the home screen.

  user = User.find(id)
  user.send_push_notification(title: false, message: false, content_available: false, count: 2)

Apple Feedback Service

Occasionally you'll want to cleanup old tokens that have been invalidated for various reasons. You can do this by running -

ActsAsPushable::APN::FeedbackService.run

We'd recommnd running this on a daily/weekly cron job.

Note: With Android this happens at the time of sending a notification, no action is required.


Setup for development

Clone the repository -

  git clone [email protected]:simpleweb/acts_as_pushable.git
  cd acts_as_pushable
  bundle install

Run the specs with RSpec

  rspec

Other useful resources

Contributing

This project follows the GitHub Flow workflow. All contributions should be provided as descriptive atomic pull requests.

The gem should be versioned in accordance to Semantic Versioning 2.0.0.