-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.rb
51 lines (42 loc) · 1.19 KB
/
notification.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# == 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
#
class Notification < ApplicationRecord
belongs_to :user
after_save :send_gcm_notification
scope :unread, -> { where read: false }
def read!
update_attributes read: true
end
def create
end
def send_gcm_notification
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
end
end
end