class NOTIFY_FCM

Attributes

body[RW]
color[RW]
icon[RW]
priority[RW]
sound[RW]
title[RW]
vibrate[RW]

Public Class Methods

new(api_key, options = {}) click to toggle source

constants NOTIFICATION_BASE_URI = 'android.googleapis.com/gcm'

# File lib/notify_fcm.rb, line 14
def initialize(api_key, options = {})
  @api_key = api_key
  @options = options
end

Public Instance Methods

build_body(registration_ids, options = {}) click to toggle source
# File lib/notify_fcm.rb, line 41
def build_body(registration_ids, options = {})
    { registration_ids: registration_ids }.merge(options)
end
message_body() click to toggle source
# File lib/notify_fcm.rb, line 45
def message_body()
   {notification: { body: body, title: title, icon: icon, sound: sound, vibrate: true, color: color, priority: priority}}
end
parse_response(response_body) click to toggle source
# File lib/notify_fcm.rb, line 53
def parse_response(response_body)
  response_data = {}
  res_body = response_body.body || {}
  case response_body.code
  when 200
    body = JSON.parse(res_body) unless res_body.empty?
    response_data[:status] = 'success'
    response_data[:response] = body || {}
  when 400
    response_data[:status] = 'failed'
    response_data[:response] = 'Json Fields are invalid'
  when 401
    response_data[:status] = 'failed'
    response_data[:response] = 'Authentication Error!'
  when 503
    response_data[:status] = 'failed'
    response_data[:response] = 'Server is temporarily unavailable.'
  when 500..599
    response_data[:status] = 'failed'
    response_data[:response] = 'FCM internal server error!'
  end
  response_data
end
response_builder(response) click to toggle source
# File lib/notify_fcm.rb, line 49
def response_builder(response)
  parse_response(response)
end
send(registration_ids, options = {})
Alias for: send_notification
send_notification(registration_ids, options = {}) click to toggle source
# File lib/notify_fcm.rb, line 19
def send_notification(registration_ids, options = {})

raise ArgumentError, 'Api Key not Found' unless !@api_key.nil?
raise ArgumentError, 'Body is missing' unless !body.nil?
raise ArgumentError, 'DeviceToken not Found' unless !registration_ids.empty?

  message = message_body()
  body = build_body(registration_ids, message)
  params = {
    body: body.to_json,
    headers: {
      'Authorization' => "key=#{@api_key}",
      'Content-Type' => 'application/json'
    }
  }

  response = self.class.post('/send', params.merge(@options))
  response_builder(response)
end
Also aliased as: send