Class: PushNotification::GcmSender

Inherits:
Object
  • Object
show all
Defined in:
lib/push_notification/gcm_sender.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, client_public_key, auth, server_key_pair) ⇒ GcmSender

Returns a new instance of GcmSender.



3
4
5
6
7
8
9
10
11
# File 'lib/push_notification/gcm_sender.rb', line 3

def initialize(endpoint, client_public_key, auth, server_key_pair)
  @endpoint = URI.parse(endpoint)
  @salt = OpenSSL::BN.rand(128).to_s(2)
  @auth = auth
  @client_public_key = client_public_key
  @server_key_pair = server_key_pair
  @server_public_key = @server_key_pair.public_key.to_bn.to_s(2)
  @shared_key = @server_key_pair.dh_compute_key(client_public_key)
end

Instance Method Details

#send(data = '') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/push_notification/gcm_sender.rb', line 13

def send(data = '')
  header = "#{@salt}\x00\x00\x10\x00\x41#{@server_public_key}"
  msg = encrypt_msg(data)
  headers = {
    'Content-Type': 'application/octet-stream',
    'Crypto-Key': "p256ecdsa=#{Base64.urlsafe_encode64(@server_public_key)}",
    'Content-Encoding': 'aes128gcm',
    'Content-Length': (header.length + msg.length).to_s,
    'TTL': '86400',
    'Authorization': "WebPush #{signature()}"
  }

  http = Net::HTTP.new(@endpoint.host, @endpoint.port)
  http.use_ssl = true
  return http.request_post(@endpoint.path, header + msg, headers)
end