Send Android notification when new notification is created
This commit is contained in:
parent
fef0bcb92c
commit
b037dccd43
3 changed files with 30 additions and 1 deletions
|
@ -12,10 +12,38 @@
|
||||||
|
|
||||||
class Notification < ActiveRecord::Base
|
class Notification < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
after_save :send_gcm_notification
|
||||||
|
|
||||||
scope :unread, -> { where read: false }
|
scope :unread, -> { where read: false }
|
||||||
|
|
||||||
def read!
|
def read!
|
||||||
update_attributes read: true
|
update_attributes read: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def send_gcm_notification
|
||||||
|
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 = { message: message }
|
||||||
|
n.priority = 'high'
|
||||||
|
n.content_available = true
|
||||||
|
n.notification = { body: message,
|
||||||
|
title: "Tabbp notification",
|
||||||
|
}
|
||||||
|
n.save!
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,7 +7,7 @@ Rpush.configure do |config|
|
||||||
# config.redis_options = {}
|
# config.redis_options = {}
|
||||||
|
|
||||||
# Frequency in seconds to check for new notifications.
|
# Frequency in seconds to check for new notifications.
|
||||||
config.push_poll = 2
|
config.push_poll = 10
|
||||||
|
|
||||||
# The maximum number of notifications to load from the store every `push_poll` seconds.
|
# The maximum number of notifications to load from the store every `push_poll` seconds.
|
||||||
# If some notifications are still enqueued internally, Rpush will load the batch_size less
|
# If some notifications are still enqueued internally, Rpush will load the batch_size less
|
||||||
|
|
|
@ -24,3 +24,4 @@ production:
|
||||||
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
|
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
|
||||||
omniauth_client_id: ""
|
omniauth_client_id: ""
|
||||||
omniauth_client_secret: ""
|
omniauth_client_secret: ""
|
||||||
|
fcm_secret: ""
|
||||||
|
|
Loading…
Reference in a new issue