class FcmPusher
Attributes
fcm_api_key[RW]
fcm_token[RW]
priority[RW]
registration_ids[RW]
Public Class Methods
new(fcm_api_key)
click to toggle source
# File lib/fcm_pusher.rb, line 21 def initialize(fcm_api_key) @fcm_api_key = fcm_api_key @priority = FcmPusher::Priority::HIGH end
Public Instance Methods
send_all(registration_ids, title, body, options = {})
click to toggle source
send notifications to more than one registered token optional parameters are icon, badge, sound and priority
# File lib/fcm_pusher.rb, line 36 def send_all(registration_ids, title, body, options = {}) @registration_ids = registration_ids body = parse_all(registration_ids, title, body, options) request(Configuration::FCM_BASE_URL, @fcm_api_key, body) end
send_once(to, title, body, options = {})
click to toggle source
send notification to a unique registered token optional parameters are icon, badge, sound and priority
# File lib/fcm_pusher.rb, line 28 def send_once(to, title, body, options = {}) @fcm_token = to body = parse_once(to, title, body, options) request(Configuration::FCM_BASE_URL, @fcm_api_key, body) end
Private Instance Methods
parse_all(registration_ids, title, body, options = {})
click to toggle source
parse json to be used in send_all
optional parameters are icon, badge, sound and priority
# File lib/fcm_pusher.rb, line 75 def parse_all(registration_ids, title, body, options = {}) options[:priority] = @priority if options[:priority].nil? { registration_ids: registration_ids, notification: { title: title, body: body, icon: options[:icon], badge: options[:badge], sound: options[:sound] }, priority: options[:priority] }.to_json end
parse_once(to, title, body, options = {})
click to toggle source
parse json to be used in send_once
optional parameters are icon, badge, sound and priority
# File lib/fcm_pusher.rb, line 57 def parse_once(to, title, body, options = {}) options[:priority] = @priority if options[:priority].nil? { to: to, notification: { title: title, body: body, icon: options[:icon], badge: options[:badge], sound: options[:sound] }, priority: options[:priority] }.to_json end
request(url, api_key, body)
click to toggle source
# File lib/fcm_pusher.rb, line 43 def request(url, api_key, body) uri = URI(url) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Post.new(uri.path, {'Content-Type': 'application/json', 'Authorization': "key=#{api_key}"}) request.body = body response = https.request(request) puts "Finished successfully with response #{response}" rescue => exception puts "Failed with #{exception}" end