docbrown/app/services/push_notifications/send.rb
Fernando Valverde d6fde3ab89
Change PushNotifications::Send to use user_ids (#13036)
* Changes PushNotifications::Send to use user_ids

* Changes devices#destroy to use unauthenticated_params

* Fix failing spec - new logic reverses order

* Apply suggestions from code review

Co-authored-by: Jamie Gaskins <jgaskins@gmail.com>

* Only send new_comment PN with Rpush behind FeatureFlag + spec tweak

* Add query order explicitly to avoid flakyness

* Review feedback - remove order clause in query & other tweaks

Co-authored-by: Jamie Gaskins <jgaskins@gmail.com>
2021-03-19 13:50:04 -06:00

29 lines
1.1 KiB
Ruby

module PushNotifications
class Send
def self.call(user_ids:, title:, body:, payload:)
new(user_ids: user_ids, title: title, body: body, payload: payload).call
end
def initialize(user_ids:, title:, body:, payload:)
@user_ids = user_ids
@title = title
@body = body
@payload = payload
end
def call
return unless FeatureFlag.enabled?(:mobile_notifications)
Device.where(user_id: @user_ids).find_each do |device|
device.create_notification(@title, @body, @payload)
end
# perform_in(30.seconds) is key: The background worker will execute Rpush.push which will send out all
# pending notifications. This means we don't need to run it for every PN. PushNotifications::DeliverWorker
# has a constraint that will execute the job only once (30 seconds from now). So if we need to send 1 PN
# every 5 seconds we only execute this once every 30s (no duplicate/unnecessary processing).
# If no PNs are scheduled to be sent for a 6h span then 0 jobs are executed.
PushNotifications::DeliverWorker.perform_in(30.seconds)
end
end
end