在 rails 后自动发送推送通知

Automatically send push notification in rails

我想在数据库记录更新时自动发送推送通知。推送通知用于 android 设备。这些记录来自 Rails 申请中的 Ruby。

任何帮助都将不胜感激。在此先致谢。

通过更新的 GCM google 尝试(firebase 云消息传递)[https://firebase.google.com/docs/cloud-messaging/] 服务。

基本上,在 update/insert 上,您可以推送消息服务以通知设备。

看看这个(问题)[

您要做的是在 table 中的记录更新时向移动设备发送推送通知。我建议您必须以可以触发推送通知的方式设计 tables 和其他所有内容。请检查以下场景。为了在客户 table 中的记录更新时向 Android 设备发送推送通知。

型号

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

异步工作者

push_worker.rb

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

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

end