GithubHelp home page GithubHelp logo

calonso / rails-push-notifications Goto Github PK

View Code? Open in Web Editor NEW
138.0 7.0 44.0 107 KB

Rails iOS, Android and Windows Phone Push Notifications made easy!!

Home Page: https://rubygems.org/gems/rails-push-notifications

License: MIT License

Ruby 100.00%

rails-push-notifications's Introduction

Rails Push Notifications

Build Status Code Climate Test Coverage

Professional iOS, Android and Windows Phone push notifications for Ruby on Rails

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.

Live example

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

Installation

To install the gem simply add this line to your Gemfile

gem 'rails-push-notifications', '~> 0.2.0'

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

Applications setup

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.

Apple Applications setup

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:

  1. Open Keychain Application of your Macintosh computer
  2. Find the recently generated certificates. Click the disclosure button on the left of your certificate and select both the certificate and the contained key
  3. 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.

Android Applications setup

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.

WindowsPhone Applications setup

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

Creating notifications

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

Apple notifications

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 } }
)

GCM Notifications

app = <Your GCM app>
notification = app.notifications.create(
  destinations: [
    'Your first destination token',
    'Your second destination token'
  ],
  data: { message: 'Hello GCM World!' }
)

WindowsPhone Notifications

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 }
)

Pushing notifications

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.

Pending tasks

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.

Contributing

  1. Fork it ( https://github.com/calonso/rails-push-notifications/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Released under the MIT-LICENSE.

rails-push-notifications's People

Contributors

arnaudremi avatar calonso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-push-notifications's Issues

APNv2

What I like about this gem is that it targets both Apple and Android platforms in one place.

Seeing as Apple rolled out and now recommends HTTP/2 based APNv2 and Google now recommends Firebase, I'm curious if there's a timeline for supporting these developments?

No text in the notification

Insertion
=> [#<RailsPushNotifications::Notification:0x007f1a15c33878 id: 7, destinations: ["ciJie4Om2aw:APA91bHckWobcMLNzOXxe9eXl0X4FuoDym9IlcBjvWOiwjtq0LptHEdy0ubgUahqb-79TF6X5J9eUUL-TRo3jACQBGmL1vPhP9cSb2TXnVCAU-iIlvQc1Kps-Yg0aUiVhjXZHBAq04Po"], app_id: 7, app_type: "RailsPushNotifications::GCMApp", data: {:text=>"Awesome bags is in Live In near you"}, results: #<RubyPushNotifications::GCM::GCMResponse:0x007f1a15824098 @canonical_ids=0, @failed=0, @results=[#<RubyPushNotifications::GCM::GCMResultOK:0x007f1a154037c0>], @success=1>, success: 1, failed: 0, sent: true, created_at: Thu, 28 Jul 2016 09:50:22 UTC +00:00, updated_at: Thu, 28 Jul 2016 09:50:22 UTC +00:00>]

Notification is receiving but no text is showing in the notification.

PFA.
screenshot_2016-07-28-15-35-42

Data for GCM is not text

Hello.

Just lost few hours.
The data object for GCM is not using "text" key but "message".

Hope it will help.

Migration files in Rails 5 wont migrate

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class CreateRailsPushNotificationsApps < ActiveRecord::Migration[4.2]
C:/Users/micha/Documents/WebDev/sms/db/migrate/20180611231832_create_rails_push_notifications_apps.rb:1:in `<top (required)>'

Caused by:
StandardError: Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class CreateRailsPushNotificationsApps < ActiveRecord::Migration[4.2]
C:/Users/micha/Documents/WebDev/sms/db/migrate/20180611231832_create_rails_push_notifications_apps.rb:1:in `<top (required)>'
Tasks: TOP => db:migrate

The fix is to specify the version in the inheritance signature:
class CreateRailsPushNotificationsApps < ActiveRecord::Migration[5.0]
and
class CreateRailsPushNotificationsNotifications < ActiveRecord::Migration[5.0]

Mulitple Notifications for the same notification

Thank you for your awesome gem !

When calling app.push_notifications, I get a response success and it works perfectly
However, i receive the same notification several Times on iPhone ( not on android and Windows Phone )

First One => 1 seconde after sending the notification
Second one => 1.5 secondes later
Third one => 1.7 secondes later
...
...
I am using ruby 2.2.1p85, Rails 4.1.0 please suggest what could be the issue.

Errno::ECONNRESET PushNotificationController -- Connection reset by peer

I am getting "Connection reset by peer" since yesterday and not able to push notifications on apns. It was working fine earlier, don't know how to solve the error.
Confirmed certificates being used are proper and active.
I am using OpenSSL 1.0.1k-fips, ruby 2.2.1p85, gem 2.4.8, Rails 4.2.4. please suggest what could be the issue.

this gem works with FCM?

if yes, how should I implement it with firebase cloud messaging? or its outdated? im using rails 5. thanks in advance..

Rails 4 support

Do you think to provide rails 4 support for this gem?

Recently i tried to include your gem in a project and i got the following trace error when i run

rails g ror_push_notifications:migrations
/Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/activerecord-4.1.7/lib/active_record/dynamic_matchers.rb:26:in `method_missing': undefined method `attr_accessible' for #<Class:0x007fbf2a1461a8> (NoMethodError)
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/bundler/gems/ror-push-notifications-4aea09c92ffd/lib/ror_push_notifications/app/models/rpn/apns_config.rb:9:in `<class:ApnsConfig>'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/bundler/gems/ror-push-notifications-4aea09c92ffd/lib/ror_push_notifications/app/models/rpn/apns_config.rb:1:in `<top (required)>'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/bundler/gems/ror-push-notifications-4aea09c92ffd/lib/ror_push_notifications.rb:2:in `<top (required)>'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0@global/gems/bundler-1.7.13/lib/bundler/runtime.rb:76:in `require'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0@global/gems/bundler-1.7.13/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0@global/gems/bundler-1.7.13/lib/bundler/runtime.rb:72:in `each'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0@global/gems/bundler-1.7.13/lib/bundler/runtime.rb:72:in `block in require'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0@global/gems/bundler-1.7.13/lib/bundler/runtime.rb:61:in `each'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0@global/gems/bundler-1.7.13/lib/bundler/runtime.rb:61:in `require'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0@global/gems/bundler-1.7.13/lib/bundler.rb:134:in `require'
    from /Users/carlosleon/sources/solid-finger/config/application.rb:7:in `<top (required)>'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/spring-1.3.2/lib/spring/application.rb:82:in `require'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/spring-1.3.2/lib/spring/application.rb:82:in `preload'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/spring-1.3.2/lib/spring/application.rb:143:in `serve'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/spring-1.3.2/lib/spring/application.rb:131:in `block in run'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/spring-1.3.2/lib/spring/application.rb:125:in `loop'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/spring-1.3.2/lib/spring/application.rb:125:in `run'
    from /Users/carlosleon/.rvm/gems/ruby-2.2.0/gems/spring-1.3.2/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/carlosleon/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/carlosleon/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'

Unable to set sender_id for GCM

When calling app.push_notifications I get an response error complaining about a mismatch sender id. @results=[#<RubyPushNotifications::GCM::GCMResultError:0x007fdd001805d0 @error="MismatchSenderId">]>,. From the documentation there doesn't seen to be a way to set this.

Typo in docs under apple application setup

"To convert the certificates the first you need is to export them as .p12 files. To do this:"

Should be:

""To convert the certificates the first thing you need to do is to export them as .p12 files. To do this:"

Send additional data using this gem

How can you send additonal data like these:

{
  "to": "<token>",
  "time_to_live": 86400,
  "collapse_key": "new_message",
  "delay_while_idle": false,
  "notification": {
    "title": "title",
    "body": "this is a noisy test",
    "tag": "new_message",
    "icon": "new_message",
    "color": "#18d821",
    "sound": "default"
  }
}

using your gem?
Thanks

Which fields are available?

Hello,
I'm testing this gem with GCM, using on the device the GCM QuickStart, in the notification created I use, as data an hash like this:

data: { message: self.title }

It correctly sends the notification, which is displayed on the android device, I was looking for a way to set the title and the icon, but can't seem to find one, I tried something like:

data: { title: "This Title", message: self.title }

but it just shows the message? What am I missing?

thank you for the answer,
Gabriele

Notifications not received when app is closed

Hello,

I am trying to send notifications to android device. When app is open in my device, i am getting push notification, but its closed there is no notifications receiving. Here is the code which i am doing:

notification = app.notifications.build(
    destinations: [DEVICE_TOKEN_HERE],
    data: { text: message, key: value, key: value }
  )
  app.push_notifications if notification.save

where key are the extra parameters i want to pass. This code works when the app is opened. Is there anything wrong in the code ? or do i need to add some other settings to receive the notification when app is closed? Your kind help is highly appreciated.

Thanks in advance.

How to handle badge count for multiple destinations

Hi You created awesome gem but in your gem and in APNS code

app =
notification = app.notifications.create(
destinations: [
'Your first destination token',
'Your second destination token'
],
data: { aps: { alert: 'Hello APNS World!', sound: 'true', badge: 1 } }
)

you are sending badge 1 for all destinations but if some one wants to send different badge count for different destinations. How did you handle this scenario.

Unauthorized Error 401

I get the following error when trying to push the notifications:

RubyPushNotifications::GCM::GCMAuthError: <HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

Update Badge number on Android

Hi, can I change the badge# on Android, I tried to pass the badge attribute but in the notification but nothing happened.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.