simpleweb/acts_as_pushable

Name: acts_as_pushable

Owner: Simpleweb

Description: Add iOS & Android device and push notification support in your Rails application.

Created: 2016-06-06 16:30:51.0

Updated: 2017-11-01 17:49:46.0

Pushed: 2017-03-06 10:38:47.0

Homepage:

Size: 290

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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

m 'acts_as_pushable'
ndle install

or just install it -

m install acts_as_pushable
Quick start

Run the generator to create the migrations and initializer template -

ils generate act_as_pushable:install
ke 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 -

ass User < ActiveRecord::Base
acts_as_pushable
d
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.
    enssl 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 -
  5. config/acts_as_pushable/apn/production.pem
  6. 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 -

    AsPushable.configure do |config|
    ig.gcm_key = 'replace_me_with_your_api_key'
    
    
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.

er = User.create
er.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

er = User.find(id)
er.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
er = User.find(id)
vice = user.devices.first

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

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.

er = User.find(id)
er.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 -

AsPushable::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 -

t clone git@github.com:simpleweb/acts_as_pushable.git
 acts_as_pushable
ndle install
Run the specs with RSpec
pec
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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.