[code language=”ruby”]
require ‘uri’
require ‘net/http’
class XploraJob < ActiveJob::Base
queue_as :default
def perform
tokens = UserDevice.pluck(:device_id)
url = URI("https://fcm.googleapis.com/fcm/send")
if tokens.present?
tokens.each do |device_id|
begin
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
request["content-type"] = ‘application/json’
request["authorization"] = ‘key=<strong>Your key here</strong>’
mydata = { ‘to’: device_id, ‘notification’: { body: ‘How are you ?’, title: ‘xploralab’}, data: { ‘link’: ‘http://xploralab.com’} }
request.body = mydata.to_json()
response = http.request(request)
rescue
end
end
end
end
end
[/code]