Project

jera_push

0.01
A long-lived project that still receives updates
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
2023
2024
 Dependencies

Development

~> 13.0.6

Runtime

>= 0.19.0, < 1.0
>= 2.4
>= 0.21
>= 4.5.1
>= 1.2.2
>= 7.1
 Project Readme

Jera Logo

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

It's composed for:

  • Services: Services to send notifications.
  • 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

Warning

  • Device registration in Firebase topics is disabled for now. Therefore, topic pushes are also temporarily disabled

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"]
  config.project_name = "YOUR_PROJECT_NAME"
  config.credentials_path = "YOUR_CREDENTIALS_PATH" #https://firebase.google.com/docs/cloud-messaging/migrate-v1?hl=pt-br#provide-credentials-manually


  ######################################################
  # 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_development'

  # 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.


Sending your notifications

  • Firs of all, create your device"
# considering your push is to a User model

token = "DEVICE_TOKEN_GENERATED_BY_APP"
pushable = User.last
platform = :android # enum [:android, :ios]
JeraPush::Device.create(token: token, platform: platform, pushable: pushable)

Sending one push for one device

  • Values inside of data need to be a string
send_to_device = JeraPush::Services::SendToDeviceService.new(
  device: JeraPush::Device.last,
  title: 'Notification Title',
  body: 'Notification Body', 
  data: { kind: :some_kind_to_something_in_app, resource_id: '3' }
)
send_to_device.call
  • If you need to specify some android os ios configuration you can pass a android or ios hash like this:
send_to_device = JeraPush::Services::SendToDeviceService.new(
  device: JeraPush::Device.last, 
  title: 'Notification Title',
  body: 'Notification Body', 
  data: { kind: :some_kind_to_something_in_app, resource_id: '3' }, 
  android: {}, 
  ios: {}
)
send_to_service.call
  • The default config to Android is: { priority: 'high'}
  • And for iOS is:
{
  headers: {
    'apns-priority': '5'
  },
  payload: {
    aps: {
      'content-available': 1
    }
  }
}
  • So, if you send some params in ios hash, the default config is will be overridden
  • And if you send android params, they has been merged with the default params

Sending one push for many devices

send_to_device = JeraPush::Services::SendToDevicesService.new(
  devices: JeraPush::Device.where('id < 10'),
  title: 'Notification Title',
  body: 'Notification Body', 
  data: { kind: :some_kind_to_something_in_app, resource_id: '3' }
)
send_to_device.call
  • If you need to specify some android os ios configuration you can pass a android or ios hash like this:
send_to_device = JeraPush::Services::SendToDevicesService.new(
  device: JeraPush::Device.where('id < 10'), 
  title: 'Notification Title', 
  body: 'Notification Body', 
  data: { kind: :some_kind_to_something_in_app, resource_id: '3' }, 
  android: {}, 
  ios: {}
)
send_to_device.call

Firebase::Client

Class responsible for interact with Firebase.

Methods

  • send_to_device
  • add_device_to_topic
  • add_devices_to_topic
  • remove_device_from_topic

Device registration in Firebase topics is disabled for now. Therefore, topic pushes are also temporarily disabled

Initialize the firebase client

client = JeraPush::Firebase::Client.new

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

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

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"