class ActsAsHocPushable::PushNotification

Attributes

data[RW]
devices[RW]
message[RW]
title[RW]

Public Class Methods

new(devices:, title:, message:, **data) click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 8
def initialize(devices:, title:, message:, **data)
  @devices = devices
  @title = title
  @message = message
  @data = data
end
send_push_notification(devices:, title: nil, message: nil, **data) click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 24
def self.send_push_notification(devices:, title: nil, message: nil, **data)
  new(devices: devices, title: title, message: message, **data).perform
end
send_push_notification_to_topic(topic:, title: nil, message: nil, **data) click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 15
def self.send_push_notification_to_topic(topic:, title: nil, message: nil, **data)
  new(devices: nil, title: title, message: message, **data).perform_topic(topic: topic)
end
send_silent_push_notification(devices:, **data) click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 28
def self.send_silent_push_notification(devices:, **data)
  silent_data = data.merge({ content_avaiable: true })
  new(devices: devices, title: nil, message: nil, **silent_data).perform
end
send_silent_push_notification_to_topic(topic:, **data) click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 19
def self.send_silent_push_notification_to_topic(topic:, **data)
  silent_data = data.merge({ content_avaiable: true })
  new(devices: nil, title: nil, message: nil, **silent_data).perform_topic(topic: topic)
end

Public Instance Methods

perform() click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 38
def perform
  tokens = @devices.map {|d| d.token }
  response = client.send(tokens, push_options)
  handle_response(response)
end
perform_topic(topic:) click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 33
def perform_topic(topic:)
  response = client.send_to_topic(topic, push_options)
  handle_response(response)
end

Protected Instance Methods

push_options(options = {}) click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 50
def push_options(options = {})
  options = {
    priority: 'high',
    data: data
  }.merge(options)
  options[:notification] = { body: @message, title: title } unless @message.nil?
  options
end

Private Instance Methods

client() click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 61
def client
  FCM.new(ActsAsHocPushable.configuration.firebase_key)
end
handle_response(response) click to toggle source

Handles the response from firebase. Invalidates failed tokens

# File lib/acts_as_hoc_pushable/push_notification.rb, line 68
def handle_response(response)
  Rails.logger.info "Raw response from fcm: #{response}" if (ActsAsHocPushable.configuration.debug_firebase_response ||= false)
  body = parse_json(response[:body])
  return if body.nil?
  Rails.logger.info "Parsed response from fcm: #{body}" if (ActsAsHocPushable.configuration.debug_firebase_response ||= false)
  failure = body['failure'].to_i
  if failure > 0
    i = 1
    results = body['results']
    results.each do |r|
      invalidate_token_at_pos(i) if r.has_key? 'error'
      i += 1
    end
  end
end
invalidate_token_at_pos(pos) click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 88
def invalidate_token_at_pos(pos)
  @devices.first(pos).last.invalidate if pos <= @devices.count
end
parse_json(json) click to toggle source
# File lib/acts_as_hoc_pushable/push_notification.rb, line 84
def parse_json(json)
  JSON.parse(json) rescue nil if json && json.length >= 2
end