Project

jera_push

0.01
No release in over a year
This gem is for send push messages via firebase and track their status.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
 Dependencies

Development

~> 1.3

Runtime

~> 2.2
~> 0.16
~> 1.1
>= 5.0.0
 Project Readme

Jera Logo

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 add multiple models . 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"
  #Update this for every new model
  config.resources_name = ["User"]

  ######################################################
  # Resource attribute showed in views                 #
  # IMPORTANT: All models need to have this attributes #
  # 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.send_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
resource_type String Model name which will have the device, needs to be the same of class. If not passed, the first model will be selected

Request

Header

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

Body

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

Response | Status: 200

{
  "data": {
    "id": 1,
    "pushable_id": 10,
    "pushable_type": "Driver",
    "token": "804b56b7ab9cdf43fff540c5d93f3922aeaf65feb14f7ae88698b9b032a7a934",
    "platform": "android",
    "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,
    "pushable_id": 10,
    "pushable_type": "Driver",
    "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",
    "pushable_id": 10,
    "pushable_type": "Driver",
    "created_at": "2016-10-17T14:19:58.776Z",
    "updated_at": "2016-10-17T20:30:20.064Z"
  },
  "status": "success"
}

Error | Status: 404

No Content

Traduções (Fazendo os dois as traduções irão funcionar corretamente!)

Para o admin ficar com as traduções corretas, adicione as traduções do do modelo e dos atributos do modelo Caso o resource_attributes tenha campos diferentes de name ou email, adicione no arquivo jera_push.pt-BR.yml, dentro de admin.attributes, exemplos logo abaixo.

Tradução do nome do modelo e atributos

  activerecord:
    attributes:
      user:
        id: '#'
        name: Nome
        email: E-mail
    models:
      user: Usuário

Tradução de atributos para o admin jera_push

  jera_push:
    admin:
      attributes:
        phone: "Telefone"
        cpf: "CPF"