Code Monkey home page Code Monkey logo

wor-push-notifications-aws's Introduction

Wor::Push::Notifications::Aws

Gem Version Dependency Status Build Status Code Climate Test Coverage

Provide basic setup for storing device tokens and sending Push Notifications to your application using AWS Simple Notification Service (SNS).

Installation

Add this line to your application's Gemfile:

gem 'wor-push-notifications-aws'

And then execute:

$ bundle

Or install it yourself as:

$ gem install wor-push-notifications-aws

Configuration

To use the gem, firstly we have to configure it. But don’t worry since the configuration simply consists of these two steps:

  1. Firstly, under the config/initializers dir, create the file wor_push_notifications_aws.rb:
Wor::Push::Notifications::Aws.configure do |config|
  config.device_types = [:ios, :android]       		# optional
  config.table_name = 'users'                 		# optional
  config.aws_region = 'us-east-1'
  config.aws_android_arn = 'some:android:arn'     # mandatory field if you choose to use Android devices
  config.aws_ios_arn = 'some:ios:arn'             # mandatory field if you choose to use iOS devices
  config.aws_ios_sandbox = true                   # mandatory field if you choose to use iOS devices
end

If you don't know where to get the arn values, please see SNS Setup section.

  1. The following step involves running the install generator, which basically creates a migration file to add a column to your selected table in order to store the tokens. To run the generator, run the following commands:
$ rails generate wor:push:notifications:aws:install
$ rake db:migrate

Usage

Note: If you haven’t configured sns, now it’s the moment. (See SNS Setup).

So far we have the gem and sns configured, so let’s move to what the gem can do. As it’s purpose is to make the app send push notifications, there are 3 methods, add_token, delete_token and send_message, to add/delete the device_token, and to send the message.

The device token is a unique token obtained from each pair of App and Device. This token is used for creating an endpoint to identify your users' phones to which you will be sending the push notifications.

The device type identifies from which type of device you have obtained the device token, because the data used for each platform is different.
iOS and Android are the only device types supported for the moment.

Add token

Attach device_tokens to a given user instance:

Wor::Push::Notifications::Aws.add_token(user, device_token, device_type)

Parameters

  • user: Instance where we want to store the device_token, to which we will send push notifications.
  • device_token: Unique identifier you get from the the app.
  • device_type: So far we support the values :android or :ios

Delete token

Delete token from the user instance:

Wor::Push::Notifications::Aws.delete_token(user, device_token)

Parameters

  • user: Instance where we want to store the device_token, to which we will send push notifications.
  • device_token: Unique identifier you get from the the app.

Send message

Send a given message to the user instance:

Wor::Push::Notifications::Aws.send_message(user, message_content)

Parameters

  • user: Instance which will receive the message. It must have the device_tokens from the user's phones.
  • message_content: Message you want to send to the user. This parameter must have a JSON format.
    • It requires the message field.
    • You can add a badge field (integer type) to be included in the app icon to show how many pending notifications the user has.
    • You can include any other field in the JSON, with the information you need to send in the push notification

*BADGE: iOS shows the badge automatically, but you have to include it yourself in Android devices.

message_content example:

Suppose you have a billing application, you will send the data related to the new bill that has been charged in the system and the amount of unread notifications.

message_content = { message: 'You have a new bill!', badge: 5, account_id: 93, bill_id: 45 }

AWS Credentials

In order to use Aws SNS, you'll need to have Aws configured with the right credentials. You can set up them according to one of the ways explained under the "credentials" section at http://docs.aws.amazon.com/sdkforruby/api/Aws/SNS/Client.html. We recommend to use ENV variables with Rails secrets when setting up the configuration.

Note: Aws region is specified in the initializer file.

SNS Setup

If you HAVE a SNS Application follow this instructions

  • i) Log in the AWS Console
  • ii) Click on Services and select Simple Notification Services in Messaging group
  • iii) Select Applications tab in the left panel
  • iv) You will have your applications' ARN listed in a table
  • v) When selecting any of them you can see more details of each application, with the information if it's a SANDBOX environment in the iOS case

You will have to create an instance profile for the Elastic Beanstalk environment that's running your application, with the permissions to access the Simple Notification Service or get an AWS_ACCESS_KEY and AWS_SECRET_KEY pair to access this service from outside of AWS.

If you want more information about AWS SNS visit the documentation page.
If you want more information on how to use AWS with Google Cloud Messaging (for Android) check this documentation.
If you want more information on how to use AWS with Apple Push Notifications Service (for iOS) check this documentation.

Requirements

Since a json attribute is needed to store device_tokens on the user table, Postgres 9.3 or higher is required.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Run rubocop lint (bundle exec rubocop -R --format simple)
  5. Run rspec tests (bundle exec rspec)
  6. Push your branch (git push origin my-new-feature)
  7. Create a new Pull Request

About

This project is maintained by Leandro Masello along with Francisco Landino and it was written by Wolox. Wolox

License

wor-push-notifications-aws is available under the MIT license. Copyright (c) 2017 Wolox

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

wor-push-notifications-aws's People

Contributors

plandino avatar santisamra avatar

Stargazers

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

Watchers

 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

wor-push-notifications-aws's Issues

Add Badge option

We need a badge field in the message contento to show when a push notification arrives.

Validate existence of table before generating migration

The main case will be the user that has the application already set up and it wants to add the push notifications feature. Considering that, we suppose that the :users table already exists and then the migration generated by this gem will run successfully.

However, we should consider that the user might not have the table before running the migration and then the rails db:migrate will fail. It may be because the user hasn't inserted the right name on the gem initializer or because it hasn't run the create table migration.

Silent Notifications

Sometimes users don't want to receive push notifications and you can't just don't send them, because in the case you are developing a chat service, the push notifications will give you the message you need to add to a certain conversation, and it is imposible in iOS to just omit them if you are sending them like we are doing them here.

iOS Documentation to send Silent notifications: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html under Configuring a Background Update Notification

Update ReadMe

Checkout the comment about the ReadMe at this PR https://github.com/Wolox/wor-paginate/pull/1/files

  • Comments about how to run the generator (rails generate wor:push:notifications:aws:install)
  • Comments about device token and device type
  • Requirements (aws) -> credentials and arn needed. Suggest to use ENV variables & secrets when setting up the configuration. Point out that the credentials have to be set up as one of the ways that appear in the "credentials" section at http://docs.aws.amazon.com/sdkforruby/api/Aws/SNS/Client.html.
  • Recommendation about notification model
  • Specify format of message_content ( { message: "bla bla"} )
  • Most important: How to use it
  • Nice to have: Tutorial to setup SNS
  • Requirements db -> Postgres >= 9.3
  • Add require 'aws-sdk-rails'

Configure and point out env variables

Update the readme with the env variables needed:

  • sns_app_arn
  • aws variables

Also, choose the correct builder according to the current configuration made.

Should we use secrets or raw ENV?

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.