2017-01-09 16:41:24 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: notifications
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# message :string
|
|
|
|
# read :boolean default(FALSE)
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
|
2017-01-09 16:22:58 +01:00
|
|
|
class Notification < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
2019-05-11 12:00:19 +02:00
|
|
|
after_save :send_gcm_notification
|
2017-01-09 16:22:58 +01:00
|
|
|
|
2017-01-14 16:22:44 +01:00
|
|
|
scope :unread, -> { where read: false }
|
|
|
|
|
2017-01-09 16:22:58 +01:00
|
|
|
def read!
|
|
|
|
update_attributes read: true
|
|
|
|
end
|
2019-05-11 12:00:19 +02:00
|
|
|
|
|
|
|
def create
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_gcm_notification
|
2019-08-04 20:30:45 +02:00
|
|
|
if !read
|
|
|
|
begin
|
|
|
|
if !Rpush::Gcm::App.find_by_name("tappb")
|
|
|
|
app = Rpush::Gcm::App.new
|
|
|
|
app.name = "tappb"
|
|
|
|
app.auth_key = Rails.application.secrets.fcm_secret
|
|
|
|
app.connections = 1
|
|
|
|
app.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
n = Rpush::Gcm::Notification.new
|
|
|
|
n.app = Rpush::Gcm::App.find_by_name("tappb")
|
|
|
|
n.registration_ids = user.android_device_registration_tokens.all.map{|r| r.token}
|
|
|
|
n.data = { body: message, title: "Tabbp notification" }
|
|
|
|
n.priority = 'high'
|
|
|
|
n.content_available = true
|
|
|
|
n.save!
|
|
|
|
rescue
|
|
|
|
end
|
2019-05-11 12:00:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
2017-01-09 16:22:58 +01:00
|
|
|
end
|