Class: FrmMercury::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/frm_mercury/sender.rb

Class Method Summary collapse

Class Method Details

.send(to = nil, title = nil, body = nil, subtitle = nil, icon = nil, sound = nil, data = nil, extra = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/frm_mercury/sender.rb', line 3

def self.send(to=nil, title=nil, body=nil, subtitle=nil, icon=nil, sound=nil, data=nil, extra={})
  config = FrmMercury.configuration

  require 'uri'
  require 'net/http'
  require 'net/https'
  require 'json'

  key = to.kind_of?(Array) ? "registration_ids" : "to"

  params = {
    "#{key}": to,
    "notification": {
      "title": title.nil? ? "Testing notification" : title,
      "body": body.nil? ? "This is a test push notification, liking it?" : body,
      "mutable_content": true,
      "sound": sound.nil? ? "enabled" : sound
    },
    "android": {
      "ttl": "86400s",
      "notification": {
        "click_action": "android.intent.action.MAIN"
      }
    },
    "data": data
  }

  if subtitle.present? 
    params[:notification][:subtitle] = subtitle
  end

  if icon.present? 
    params[:notification][:icon] = icon
  end

  params = params.merge(extra).to_json

  uri = URI.parse("https://fcm.googleapis.com/fcm/send")
  https = Net::HTTP.new(uri.host,uri.port)
  https.use_ssl = true
  req = Net::HTTP::Post.new(uri.path, initheader = {"Content-Type" => "application/json", "Authorization" => "key=#{config.get_api_key}"})
  req.body = params
  res = https.request(req)
  puts "Response #{res.code} #{res.message}: #{res.body}"
  return "Response #{res.code} #{res.message}: #{res.body}"
end