FcmRailsPushNotifications is build on top of RailsPushNotifications for supporting FCM. RailsPushNotifications is an intuitive and easy to use gem that will allow you to easily add Push Notifications to your project.
RailsPushNotifications key features:
- Multiple Apple/Android/WinPhone applications/configurations
- Single and Bulk notifications
- Fully customizable notification's contents
- Detailed feedback on each individual notification's push results.
Want to try this gem live?
Browse https://rails-push-notifications-test.herokuapp.com and play with it! The source code for that project is here: https://github.com/calonso/rails_push_notifications_test
To install the gem simply add this line to your Gemfile
gem 'fcm-rails-push-notifications', '~> 0.2.4'
and then install it by running
$ bundle install
Once you have it installed, you need to generate the database migrations in order to build the required database structure and then run the newly generated migrations. So do it by executing:
$ rails generate rails_push_notifications:migrations
$ rake db:migrate
First step when using RailsPushNotifications is to setup Apple/Android/WinPhone applications to which the notifications will be associated to. Each platform has it's own details and process.
Once you have configured your app as Apple Push Notifications Service enabled and you have requested and downloaded both the development and production certificates, it's time to convert them to a readable format (.pem) to import them into Rpn afterwards.
To convert the certificates the first thing you need to do is to export them as .p12 files. To do this:
- Open Keychain Application of your Macintosh computer
- Find the recently generated certificates. Click the disclosure button on the left of your certificate and select both the certificate and the contained key
- Right click and select 'Export 2 items...'. Then select the desired .p12 format and save it to your disk
Once exported, to convert it to the readable .pem format simply run:
$ openssl pkcs12 -in <your .p12 filename> -out <your output .pem filename>.pem -nodes -clcerts
Now it's time to create our applications
app = RailsPushNotifications::APNSApp.new
app.apns_dev_cert = File.read('path/to/your/development/certificate.pem')
app.apns_prod_cert = File.read('path/to/your/production/certificate.pem')
app.sandbox_mode = true
app.save
And that's it!! You can create as many different applications as you want. RailsPushNotifications gem allows it.
The only thing you need is the API_KEY that you will get from the Google APIS Console > API Access
app = RailsPushNotifications::GCMApp.new
app.gcm_key = '<your app api key>'
app.save
That's all. You have your applications' configurations stored in RailsPushNotification.
The only thing you need is the API_KEY that you will get from the Google APIS Console for FCM
app = RailsPushNotifications::FCMApp.new
app.fcm_key = '<your app api key>'
app.save
This case is similar but even simpler than APNS Apps
app = RailsPushNotifications::MPNSApp.new
app.cert = File.read('path/to/your/certificate.pem')
app.save
The notifications interface is common to all kind of apps, but depending on the type of underlying app the destinations and the data format will vary, so for example
app = <Your Apple app>
notification = app.notifications.create(
destinations: [
'Your first destination token',
'Your second destination token'
],
data: { aps: { alert: 'Hello APNS World!', sound: 'true', badge: 1 } }
)
app = <Your GCM app>
notification = app.notifications.create(
destinations: [
'Your first destination token',
'Your second destination token'
],
data: { message: 'Hello GCM World!' }
)
app = <Your FCM app>
notification = app.notifications.create(
destinations: [
'Your first destination token',
'Your second destination token'
],
data: { message: 'Hello FCM World!' }
)
app = <Your WindowsPhone app>
notification = app.notifications.create(
destinations: [
'Your first destination url',
'Your second destination url'
],
data: { title: 'Title', message: 'Hello MPNS World!', type: :toast }
)
Eventually we want to actually push our notifications. For that task all the different apps
have a push_notifications
method which will find the associated and not yet pushed notifications
and actually push them.
app = <Your app>
# Create several notifications for your app
app.push_notifications
As this task can be potentially very time consuming you will probably want to invoke it in a background worker
And that's all!! I hope you find this gem useful and please, feel free to contribute with any improvement/bug you may find.
Please, feel free to contribute in building any of those or adding more stuff to this list!
- Better generation of the data for each kind of notification. Hiding the particulars to avoid bugs.
- Rake task to push all notifications from all applications.
- Fork it ( https://github.com/santhums/rails-push-notifications/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Released under the MIT-LICENSE.