class Pushing::Adapters::FcmGemAdapter

Constants

SUCCESS_CODES

Attributes

server_key[R]

Public Class Methods

new(fcm_settings) click to toggle source
# File lib/pushing/adapters/fcm/fcm_gem_adapter.rb, line 14
def initialize(fcm_settings)
  @server_key = fcm_settings.server_key
end

Public Instance Methods

push!(notification) click to toggle source
# File lib/pushing/adapters/fcm/fcm_gem_adapter.rb, line 18
def push!(notification)
  json     = notification.payload
  ids      = json.delete(:registration_ids) || Array(json.delete(:to))
  response = FCM.new(server_key).send(ids, json)

  if SUCCESS_CODES.include?(response[:status_code])
    FcmResponse.new(response.slice(:body, :headers, :status_code).merge(raw_response: response))
  else
    raise "#{response[:response]} (response body: #{response[:body]})"
  end
rescue => cause
  resopnse = FcmResponse.new(response.slice(:body, :headers, :status_code).merge(raw_response: response)) if response
  error    = Pushing::FcmDeliveryError.new("Error while trying to send push notification: #{cause.message}", resopnse, notification)

  raise error, error.message, cause.backtrace
end