class NotificationHub::Channels::BrowserPushNotification::Fcm

Public Class Methods

new(options) click to toggle source
# File lib/notification_hub/channels/browser_push_notification/fcm.rb, line 9
                  def initialize(options)                                 
                          super
end
send_message(event_code, data, options) click to toggle source
# 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