From b037dccd432eeae9075a360c7c2ce32262106bb2 Mon Sep 17 00:00:00 2001 From: redfast00 Date: Sat, 11 May 2019 12:00:19 +0200 Subject: [PATCH] Send Android notification when new notification is created --- app/models/notification.rb | 28 ++++++++++++++++++++++++++++ config/initializers/rpush.rb | 2 +- config/secrets.yml | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index aed0e88..d9a2c27 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -12,10 +12,38 @@ class Notification < ActiveRecord::Base 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 !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 diff --git a/config/initializers/rpush.rb b/config/initializers/rpush.rb index 90332bc..15e89cc 100644 --- a/config/initializers/rpush.rb +++ b/config/initializers/rpush.rb @@ -7,7 +7,7 @@ Rpush.configure do |config| # config.redis_options = {} # 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. # If some notifications are still enqueued internally, Rpush will load the batch_size less diff --git a/config/secrets.yml b/config/secrets.yml index c9b7fd3..d440843 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -24,3 +24,4 @@ production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> omniauth_client_id: "" omniauth_client_secret: "" + fcm_secret: ""