Class: PushNotificationSender

Inherits:
Object
  • Object
show all
Defined in:
app/services/push_notification_sender.rb

Constant Summary collapse

REQUIRED_KEYS =
%i[title body].freeze
OPTIONAL_KEYS =
%i[click_action sound android_channel_id icon].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notification) ⇒ PushNotificationSender

Returns a new instance of PushNotificationSender.



9
10
11
# File 'app/services/push_notification_sender.rb', line 9

def initialize(notification)
  @notification = notification
end

Instance Attribute Details

#notificationObject (readonly)

Returns the value of attribute notification.



4
5
6
# File 'app/services/push_notification_sender.rb', line 4

def notification
  @notification
end

Instance Method Details

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/push_notification_sender.rb', line 13

def run
  return if notification.receiver.device_ids.blank?
  return if notification.delivered_with?(:fcm)
  return unless REQUIRED_KEYS.all? { |key| notification.respond_to?(key) }

  notification.receiver.device_ids.each do |device_id|
    fcm_client.send(message: message(device_id))
  rescue FirebaseCloudMessenger::Error
    job_class = Hertz::Fcm.deletion_job_class_name.safe_constantize
    job_class.perform_later(device_id)
  end

  notification.mark_delivered_with(:fcm)
end