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
Size: 290
Language: Ruby
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A gem for Ruby on Rails that makes managing devices and push notifications for both iOS and Android easy and reliable.
Add ActsAsPushable to your Gemfile
m 'acts_as_pushable'
ndle install
or just install it -
m install acts_as_pushable
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
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 -
enssl pkcs12 -in cert.p12 -out apple_push_notification.pem -nodes -clcerts
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.
Register for GCM (Google Cloud Messaging) at developers.google.com.
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'
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",
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.
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.
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)
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.
Clone the repository -
t clone git@github.com:simpleweb/acts_as_pushable.git
acts_as_pushable
ndle install
pec
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.