Class: NotificationHub::Channels::BrowserPushNotification::Fcm

Inherits:
Base
  • Object
show all
Defined in:
lib/notification_hub/channels/browser_push_notification/fcm.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Fcm

Returns a new instance of Fcm.



9
10
11
# File 'lib/notification_hub/channels/browser_push_notification/fcm.rb', line 9

def initialize(options)	 				
					super
end

Class Method Details

.send_message(event_code, data, options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/notification_hub/channels/browser_push_notification/fcm.rb', line 14

def send_message(event_code, data, options)	
	event = event_code.split(".")			
	
	begin		
		json_string = ActionController::Base.new.
			render_to_string("#{gateway_options[:template_path]}/#{event[0]}/#{event[1]}", locals: data)
		json_object = JSON.parse(json_string)
		json_object[:to] = options[:push_token]
		json_object[:registration_ids] = options[:push_tokens]

		response = HTTParty.post("https://fcm.googleapis.com/fcm/send", { 
	    :body => json_object.to_json,
	    :headers => { 
	    	'Content-Type' => 'application/json',
	    	'Authorization' => "key=#{gateway_options[:server_key]}"
	    },
	    :timeout => gateway_options[:timeout_time]
	  })		

	  raise "BrowserPushNotification::FCM Error: #{response.body}" if response.code != 200
	rescue => exception						  
	  raise "BrowserPushNotification::FCM Error: #{exception.message}"
	end							
end