Code Monkey home page Code Monkey logo

jera-push's Introduction

JeraPush is a easy tool to work with push messages and firebase API.

It's composed for:

  • Device: model responsible for register and interact with device tokens.
  • Message: model responsible for register the message content and status after sent.
  • MessageDevice: model responsible for connect the message sent and the target devices.
  • Firebase::Client: class responsible for interact with Firebase.

Features

  • Send push messages with Firebase
  • Rest Routes to register and remove devices
  • Web Interface

Getting started

Add it to your Gemfile:

gem 'jera_push'

Run the bundle install command to install it.

Next, you need to run the generator and inform the model to associate with devices. You can do it with this command:

$> rails generate jera_push MODEL_NAME

That command will create the necessary migrations and the initialize file. The file will be like this:

#this is the intilizer
#here you will set up the jera push configuration
JeraPush.setup do |config|
  config.firebase_api_key = "YOUR_API_KEY"
  config.resource_name = "User"

  # Resource attribute showed in views
  # config.resource_attributes = [:email, :name]

  # Topic default
  # You should put with your environment
  config.default_topic = 'jera_push_staging'

  # Admin credentials
  # config.admin_login = {
  #   username: 'jera_push',
  #   password: 'JeraPushAdmin'
  # }
end

You has to change the default_topic for your environment, because that's the topic that a brodcast sends a message, and it wouldn't be the same in diferents environment


Scheduling Messages

This gem doesn't support scheduled messages yet. For it, you need implement your own solution with another service like Sidekiq, Whenever, Rufus or other.


Firebase::Client

Class responsible for interact with Firebase.

Methods

  • instance static
  • add_device_to_topic
  • add_devices_to_topic
  • remove_device_from_topic

instance()

Gets the client instance.

client = JeraPush::Firebase::Client.instance

add_device_to_topic(topic: String, device: Object)

Subscribe the device to topic.

client = JeraPush::Firebase::Client.instance
client.add_device_to_topic(topic: 'your_topic', device: JeraPush::Device.first)

add_devices_to_topic(topic: String, devices: Array)

Subscribe the devices to topic.

client = JeraPush::Firebase::Client.instance
client.add_devices_to_topic(topic: 'your_topic', devices: JeraPush::Device.last(5))

remove_device_from_topic(topic: String, devices: Array)

Unsubscribe the devices to topic.

client = JeraPush::Firebase::Client.instance
client.remove_device_from_topic(topic: 'your_topic', devices: JeraPush::Device.last(5))
client.remove_device_from_topic(topic: 'your_topic', devices: [JeraPush::Device.last]) # For one object

Device

Model responsible for register and interact with device tokens to send push messages.

Attributes

Attribute Type Description
Token String Token for target device
Platform Enumerize Type of device platform. Can be :ios, :android or :chrome

Methods

  • send_message
  • subscribe
  • unsubscribe

send_message(Hash)

Sends push message to current device.

JeraPush::Device.first.send_message({ body: 'Hello World', title: 'Hey' })

subscribe(String)

Subscribe current device to topic.

JeraPush::Device.first.subscribe('your_topic')

unsubscribe(String)

Unsubscribes current device from topic.

JeraPush::Device.first.unsubscribe('your_topic')

Message

Model responsible for register the message content and status after sent.

Attributes

Attribute Type Description
content Text Message content
status Enumerize Message status after sending
failure_count Integer failure count after sending
success_count Integer success count after sending

Methods

  • send_broadcast static
  • send_to static
  • send_to_devices
  • send_to_device

send_broadcast(content: {})

Sends message to all registered devices.

JeraPush::Message.broadcast(content: { body: 'Hello World', title: 'Hey' })

send_to(Object or ActiveRecord_Relation, content: {})

Creates message with content and relates with object or collection, then sends push message.

  • One Object
JeraPush::Message.send_to(JeraPush::Device.first, content: { body: 'Hello World', title: 'Hey' })
  • Active Record Relation
JeraPush::Message.send_to(JeraPush::Device.where('id < 10'), content: { body: 'Hello World', title: 'Hey' })

send_to_devices(Array)

Sends current message to targets devices.

JeraPush::Message.first.send_to_devices(devices: JeraPush::Device.last(3))

send_to_device(Object)

Sends current message to one target device.

JeraPush::Message.first.send_to_device(device: JeraPush::Device.last)

API - Current Version: V1

Endpoints

Create a device

/jera_push/v1/devices | POST

Parameter Type Description
token required String Device token which will be registred
platform required String Device platform. Can be 'android', 'ios' or 'chrome'.
resource_id Integer Model object which will have the device

Request

Header

{
  "Content-Type": "application/json"
}

Body

{
  "token": "804b56b7ab9cdf43fff540c5d93f3922aeaf65feb14f7ae88698b9b032a7a934",
  "platform": "android",
  "resource_id": 10
}

Response | Status: 200

{
  "data": {
    "id": 1,
    "token": "804b56b7ab9cdf43fff540c5d93f3922aeaf65feb14f7ae88698b9b032a7a934",
    "platform": "android",
    "resource_id": 10,
    "created_at": "2016-10-17T14:19:58.776Z",
    "updated_at": "2016-10-17T20:30:20.064Z"
  },
  "status": "success"
}

Error | Status: 422

{
  "data": {
    "id": null,
    "token": null,
    "platform": null,
    "resource_id": 10,
    "created_at": null,
    "updated_at": null
  },
  "errors": [
    "Token não pode ficar em branco",
    "Platform não pode ficar em branco"
  ],
  "status": "unprocessable_entity"
}

Delete a device

/jera_push/v1/devices | DELETE

Parameter Type Description
token String Target device which will be deleted
platform String Device platform. Can be 'android', 'ios' or 'chrome'.

Request

Header

{
  "Content-Type": "application/json"
}

Body

{
  "token": "804b56b7ab9cdf43fff540c5d93f3922aeaf65feb14f7ae88698b9b032a7a934",
  "platform": "android"
}

Response | Status: 200

{
  "data": {
    "id": 1,
    "token": "804b56b7ab9cdf43fff540c5d93f3922aeaf65feb14f7ae88698b9b032a7a934",
    "platform": "android",
    "resource_id": 10,
    "created_at": "2016-10-17T14:19:58.776Z",
    "updated_at": "2016-10-17T20:30:20.064Z"
  },
  "status": "success"
}

Error | Status: 404

No Content

jera-push's People

Contributors

lppedrohs avatar marcaoas avatar daividssilverio avatar diogobernardelli avatar lppedro avatar vitorjera avatar

Watchers

James Cloos avatar

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.