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.
- Send push messages with Firebase
- Rest Routes to register and remove devices
- Web Interface
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
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.
Class responsible for interact with Firebase.
- instance
static
- add_device_to_topic
- add_devices_to_topic
- remove_device_from_topic
Gets the client instance.
client = JeraPush::Firebase::Client.instance
Subscribe the device to topic.
client = JeraPush::Firebase::Client.instance
client.add_device_to_topic(topic: 'your_topic', device: JeraPush::Device.first)
Subscribe the devices to topic.
client = JeraPush::Firebase::Client.instance
client.add_devices_to_topic(topic: 'your_topic', devices: JeraPush::Device.last(5))
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
Model responsible for register and interact with device tokens to send push messages.
Attribute | Type | Description |
---|---|---|
Token | String | Token for target device |
Platform | Enumerize | Type of device platform. Can be :ios , :android or :chrome |
- send_message
- subscribe
- unsubscribe
Sends push message to current device.
JeraPush::Device.first.send_message({ body: 'Hello World', title: 'Hey' })
Subscribe current device to topic.
JeraPush::Device.first.subscribe('your_topic')
Unsubscribes current device from topic.
JeraPush::Device.first.unsubscribe('your_topic')
Model responsible for register the message content and status after sent.
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 |
- send_broadcast
static
- send_to
static
- send_to_devices
- send_to_device
Sends message to all registered devices.
JeraPush::Message.broadcast(content: { body: 'Hello World', title: 'Hey' })
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' })
Sends current message to targets devices.
JeraPush::Message.first.send_to_devices(devices: JeraPush::Device.last(3))
Sends current message to one target device.
JeraPush::Message.first.send_to_device(device: JeraPush::Device.last)
/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"
}
/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