8000 GitHub - rpush/rpush: The push notification service for Ruby.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

rpush/rpush

Folders and files

8000
NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gem Version RPush Test Test Coverage Code Climate Join the chat at https://gitter.im/rpush/rpush

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

Feature Highlights

Getting Started

Add it to your Gemfile:

gem 'rpush'

Initialize Rpush into your project. Rails will be detected automatically.

$ cd /path/to/project
$ bundle
$ bundle exec rpush init

Create an App & Notification

Apple Push Notification Service

There is a choice of two modes, using certificates or using tokens:

If this is your first time using the APNs, you will need to generate either SSL certificates (for standard Apns) or an Encryption Key (p8) and an Encryption Key ID (for Apnsp8). See Generating Certificates for instructions.

Apnsp8

To use the p8 APNs Api:

app = Rpush::Apnsp8::App.new
app.name = "ios_app"
app.apn_key = File.read("/path/to/sandbox.p8")
app.environment = "development" # APNs environment.
app.apn_key_id = "APN KEY ID" # This is the Encryption Key ID provided by apple
app.team_id = "TEAM ID" # the team id - e.g. ABCDE12345
app.bundle_id = "BUNDLE ID" # the unique bundle id of the app, like com.example.appname
app.connections = 1
app.save!
n = Rpush::Apnsp8::Notification.new
n.app = Rpush::Apnsp8::App.find_by_name("ios_app")
n.device_token = "..." # hex string
n.alert = "hi mom!"
# n.alert = { title: "push title", subtitle: "more to say", body: "hi mom!" }
n.data = { foo: :bar }
n.save!
Apns2

(NB this uses the same protocol as Apnsp8, but authenticates with a certificate rather than tokens)

app = Rpush::Apns2::App.new
app.name = "ios_app"
app.certificate = File.read("/path/to/sandbox.pem")
app.environment = "development"
app.password = "certificate password"
app.bundle_id = "BUNDLE ID" # the unique bundle id of the app, like com.example.appname
app.connections = 1
app.save!
n = Rpush::Apns2::Notification.new
n.app = Rpush::Apns2::App.find_by_name("ios_app")
n.device_token = "..." # hex string
n.alert = "hi mom!"
# n.alert = { title: "push title", subtitle: "more to say", body: "hi mom!" }
n.data = {
  headers: { 'apns-topic': "BUNDLE ID" }, # the bundle id of the app, like com.example.appname. Not necessary if set on the app (see above)
  foo: :bar
}
n.save!

You should also implement the ssl_certificate_will_expire reflection to monitor when your certificate is due to expire.

Safari Push Notifications

Using one of the notifications methods above, the url_args attribute is available for Safari Push Notifications.

Environment

The app environment for any Apns* option is "development" for XCode installs, and "production" for app store and TestFlight. Note that you can now use one (production + sandbox) certificate (you don't need a separate "sandbox" or development certificate), but if you do generate a development/sandbox certificate it can only be used for "development". With Apnsp8 tokens, you can target either "development" or "production" environments.

Firebase Cloud Messaging

You will need two params to make use of FCM via Rpush.

  • firebase_project_id - The Project number in your Firebase Project Settings
  • json_key - The JSON key file for a service account with the Firebase Admin SDK Administrator Service Agent role.

Create service account in the google cloud account attached to your firebase account: https://console.cloud.google.com/iam-admin/serviceaccounts Make sure it has Role Firebase Admin SDK Administrator Service Agent Add + Download the json key for the service account.

Once you have those two params, you can create an FCM app and send notifications.

fcm_app = Rpush::Fcm::App.new
fcm_app.name = "fcm_app"

8000
fcm_app.firebase_project_id = "someapp-123456"
fcm_app.json_key = Rails.root.join("your/key/somewhere.json").read # or from a ENV variable - just needs to be the whole json file
fcm_app.connections = 30
fcm_app.save!
n = Rpush::Fcm::Notification.new
n.app = Rpush::Fcm::App.where(name: "fcm_app").first
n.device_token = device_token # Note that device_token is used here instead of registration_ids
n.notification = { title: "push title", body: "hi mom!" } # either title or body needs to be set, or nothing goes through
n.data = {}.transform_values(&:to_s) # All values going in here have to be strings, if you have anything else - nothing goes through
n.save!

FCM also requires you to respond to Canonical IDs.

Check the FCM reference for what keys you can use and are available to you. Note: Not all are yet implemented in Rpush.

Amazon Device Messaging

app = Rpush::Adm::App.new
app.name = "kindle_app"
app.client_id = "..."
app.client_secret = "..."
app.connections = 1
app.save!
n = Rpush::Adm::Notification.new
n.app = Rpush::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.

Windows Phone Notification Service (Windows Phone 8.0 and 7.x)

Uses the older Windows Phone 8 Toast template

app = Rpush::Wpns::App.new
app.name = "windows_phone_app"
app.client_id = # Get this from your apps dashboard https://dev.windows.com
app.client_secret = # Get this from your apps dashboard https://dev.windows.com
app.connections = 1
app.save!
n = Rpush::Wpns::Notification.new
n.app = Rpush::Wpns::App.find_by_name("windows_phone_app")
n.uri = "http://..."
n.data = {title:"MyApp", body:"Hello world", param:"user_param1"}
n.save!

Windows Notification Service (Windows 8.1, 10 Apps & Phone > 8.0)

Uses the more recent Toast template

The client_id here is the SID URL as seen here. Do not confuse it with the client_id on dashboard.

You can (optionally) include a launch argument by adding a launch key to the notification data.

You can (optionally) include an audio element by setting the sound on the notification.

app = Rpush::Wns::App.new
app.name = "windows_phone_app"
app.client_id = YOUR_SID_URL
app.client_secret = YOUR_CLIENT_SECRET
app.connections = 1
app.save!
n = Rpush::Wns::Notification.new
n.app = Rpush::Wns::App.find_by_name("windows_phone_app")
n.uri = "http://..."
n.data = {title:"MyApp", body:"Hello world", launch:"launch-argument"}
n.sound = "ms-appx:///mynotificationsound.wav"
n.save!

Windows Raw Push Notifications

Note: The data is passed as .to_json so only this format is supported, although raw notifications are meant to support any kind of data. Current data structure enforces hashes and .to_json representation is natural presentation of it.

n = Rpush::Wns::RawNotification.new
n.app = Rpush::Wns::App.find_by_name("windows_phone_app")
n.uri = 'http://...'
n.data = { foo: 'foo', bar: 'bar' }
n.save!

Windows Badge Push Notifications

Uses the badge template and the type wns/badge.

n = Rpush::Wns::BadgeNotification.new
n.app = Rpush::Wns::App.find_by_name("windows_phone_app")
n.uri = 'http://...'
n.badge = 4
n.save!

Pushy

Pushy is a highly-reliable push notification gateway, based on MQTT protocol for cross platform push notification delivery that includes web, Android, and iOS. One of its advantages is it allows for reliable notification delivery to Android devices in China where Google Cloud Messaging and Firebase Cloud Messaging are blocked and to custom hardware devices that use Android OS but are not using Google Play Services.

Note: current implementation of Pushy only supports Android devices and does not include subscriptions.

app = Rpush::Pushy::App.new
app.name = "android_app"
app.api_key = YOUR_API_KEY
app.connections = 1
app.save!
n = Rpush::Pushy::Notification.new
n.app = Rpush::Pushy::App.find_by_name("android_app")
n.registration_ids = ["..."]
n.data = { message: "hi mom!"}
n.time_to_live = 60 # seconds
n.save!

For more documentation on Pushy.

Webpush

Webpush is a protocol for delivering push messages to desktop browsers. It's supported by all major browsers (except Safari, you have to use one of the Apns transports for that).

Using VAPID, there is no need for the sender of push notifications to register upfront with push services (as was the case with the now legacy Mozilla or Google desktop push providers).

Instead, you generate a pair of keys and use the public key when subscribing users in your web app. The keys are stored along with an email address (which, according to the spec, can be used by push service providers to contact you in case of problems) in the certificates field of the Rpush Application record:

vapid_keypair = Webpush.generate_key.to_hash
app = Rpush::Webpush::App.new
app.name = 'webpush'
app.certificate = vapid_keypair.merge(subject: 'mailto:user@example.org').to_json
app.connections = 1
app.save!

The subscription object you obtain from a subscribed browser holds an endpoint URL and cryptographic keys. When sending a notification, simply pass the whole subscription as sole member of the registration_ids collection:

n = Rpush::Webpush::Notification.new
n.app = Rpush::App.find_by_name("webpush")
n.registration_ids = [subscription]
n.data = { message: "hi mom!" }
n.save!

In order to send the same message to multiple devices, create one Notification per device, as passing multiple subscriptions at once as registration_ids is not supported.

Running Rpush