Send Android notification when new notification is created

This commit is contained in:
redfast00 2019-05-11 12:00:19 +02:00
parent fef0bcb92c
commit b037dccd43
No known key found for this signature in database
GPG key ID: 5946E0E34FD0553C
3 changed files with 30 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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: ""