[Awesome Ruby Gem] Use rpush gem to push notify to notification service
Rpush. The push notification service for Ruby.
Rpush aims to be the de facto gem for sending push notifications in Ruby. Its core goals are ease of use, reliability and a rich feature set. Rpush provides numerous advanced features not found in others gems, giving you greater control & insight as your project grows. These are a few of the reasons why companies worldwide rely on Rpush to deliver their notifications.
Supported Services
-
Apple Push Notification Service
Including Safari Push Notifications.
-
Firebase Cloud Messaging (used to be Google Cloud Messaging)
-
Amazon Device Messaging
-
Windows Phone Push Notification Service
-
Pushy
-
Webpush
Feature Highlights
-
Use ActiveRecord or Redis for storage.
-
Plugins for Bugsnag, Sentry, StatsD or write your own.
-
Seamless integration with your projects, including Rails.
-
Run as a daemon, inside a job queue, on the command-line or embedded in another process.
-
Scales vertically (threading) and horizontally (multiple processes).
-
Designed for uptime - new apps are loaded automatically, signal HUP to update running apps.
-
Hooks for fine-grained instrumentation and error handling (Reflection API).
-
Tested with MRI
Installation
You can install it as a gem:
1 | gem install rpush |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Usages
Create an App & Notification
Apple Push Notification Service
There is a choice of two modes (and one legacy mode) using certificates or using tokens:
-
Rpush::Apns2 This requires an annually renewable certificate. see https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_certificate-based_connection_to_apns
-
Rpush::Apnsp8 This uses encrypted tokens and requires an encryption key id and encryption key (provide as a p8 file). (see https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token-based_connection_to_apns)
If this is your first time using the APNs, you will need to generate either SSL certificates (for Apns2 or Apns) or an Encryption Key (p8) and an Encryption Key ID (for Apnsp8). See Generating Certificates - https://github.com/rpush/rpush/wiki/Generating-Certificates for instructions.
Apnsp8
To use the p8 APNs Api:
1 | app = Rpush::Apnsp8::App.new |
1 | n = Rpush::Apnsp8::Notification.new |
Apns2
(NB this uses the same protocol as Apnsp8, but authenticates with a certificate rather than tokens)
1 | app = Rpush::Apns2::App.new |
1 | n = Rpush::Apns2::Notification.new |
Firebase Cloud Messaging
FCM and GCM are – as of writing – compatible with each other. See also this comment for further references.
Please refer to the Firebase Console on where to find your auth_key (probably called Server Key there). To verify you have the right key, use tools like Postman, HTTPie, curl or similar before reporting a new issue. See also this comment.
1 | app = Rpush::Gcm::App.new |
1 | n = Rpush::Gcm::Notification.new |
FCM also requires you to respond to Canonical IDs - https://github.com/rpush/rpush/wiki/Canonical-IDs.
Amazon Device Messaging
1 | app = Rpush::Adm::App.new |
1 | n = Rpush::Adm::Notification.new |
For more documentation on ADM - https://developer.amazon.com/sdk/adm.html.
See Create an App & Notification - https://github.com/rpush/rpush#create-an-app–notification to learn more.
Check the FCM reference - https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support for what keys you can use and are available to you. Note: Not all are yet implemented in Rpush.
Running Rpush
It is recommended to run Rpush as a separate process in most cases, though embedding and manual modes are provided for low-workload environments.
See rpush help for all available commands and options.
As a daemon
1 | cd /path/to/project |
As a foreground process
1 | cd /path/to/project |
On the command-line
1 | rpush push |
Rpush will deliver all pending notifications and then exit.
In a scheduled job
1 | Rpush.push |
See Push API for more details.
Embedded inside an existing process
1 | if defined?(Rails) |
Call this during startup of your application, for example, by adding it to the end of config/rpush.rb. See Embedding API for more details.
Configuration
See Configuration · rpush/rpush Wiki - https://github.com/rpush/rpush/wiki/Configuration for a list of options.
References
[1] rpush/rpush: The push notification service for Ruby. - https://github.com/rpush/rpush
[2] rpush | RubyGems.org | your community gem host - https://rubygems.org/gems/rpush
[3] Configuration · rpush/rpush Wiki - https://github.com/rpush/rpush/wiki/Configuration