0

I want to automatically send a push notification when ever the database record is updated. The push notifaction is for an android device. The records are from a Ruby on Rails application.

Any help will be appreciable .Thanks in advance.

user3366016
  • 1,267
  • 2
  • 18
  • 31
dhruv mehta
  • 67
  • 11
  • So… what did you try so far and where do you get stuck? – spectras Aug 31 '16 at 11:27
  • I want to update the record from the device side and send notification when record update in database with the device.not from the web application side.so fire a push notification when record update from the device – dhruv mehta Sep 01 '16 at 04:59

2 Answers2

1

Well what you are trying to do is send a push notification to a mobile device when a record in a table gets updated. I would suggest you have to design the tables and everything else in such a way that you can trigger the push notification. Please check the following scenario. In order to send a push notification to an Android device when the record in the customers table gets updated.

Model

customer.rb

class Customer < ActiveRecord::Base

  after_save :send_push_notification

  def send_push_notification
    PushWorker.perform_async({id: self.id})
  end

  def send_push
    require 'gcm'

    gcm = GCM.new(ENV['API_KEY'])

    registration_ids= Device.android.map(&:registration_id)
    options = {
      data: data,
      collapse_key: collapse_key || 'my_app'
    }

    response = gcm.send(registration_ids, options)
  end

end

Async Worker

push_worker.rb

class PushWorker
  include Sidekiq::Worker
  include Sidetiq::Schedulable

  def perform(params)
    customer = Customer.find(params[:id])
    customer.send_push
  end

end
Pragash
  • 713
  • 7
  • 9
  • I want to update the record from the device side and send notification when record update in database with the device.not from the web application side.so fire a push notification when record update from the device – dhruv mehta Sep 01 '16 at 04:59
0

Try (firebase cloud messaging)[https://firebase.google.com/docs/cloud-messaging/] service through google which is the newer GCM.

Basically on update/insert you could push to the messaging service to notify the device.

Check out this (question)[https://stackoverflow.com/a/22242606/3366016]

Community
  • 1
  • 1
user3366016
  • 1,267
  • 2
  • 18
  • 31