Class: Pushofy::Push

Inherits:
Object
  • Object
show all
Defined in:
lib/pushofy/push.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Push



11
12
13
14
15
16
# File 'lib/pushofy/push.rb', line 11

def initialize(args = {})
  @id = args[:id]
  @message = args[:message]
  @app = args[:app]
  @from = args[:from]
end

Instance Method Details

#send_pushObject



20
21
22
23
24
25
26
27
28
# File 'lib/pushofy/push.rb', line 20

def send_push
  device = Device.find(@id)
  device_type = device.device_type
  if device_type == 'IOS' && !device_type.nil?
    send_to_ios(device)
  elsif device_type == 'Android' && !device_type.nil?
    send_to_android(device)
  end
end

#send_to_android(device) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pushofy/push.rb', line 46

def send_to_android(device)
  body = {}
  arr = []
  arr << device.registration_id
  body['registration_ids'] = arr
  # body['collapse_key'] = 'Updates Available'
  #    body['registration_ids'] = registration_ids
  #    body['data'] = 'Hi this is my first push message'
  #    body['delay_while_idle'] = # true or false
  #    body['time_to_live'] = # number in seconds
  android_payload = { 'message' =>  "You have a new #{app} from #{from}" }
  android_payload['app'] = @app
  android_payload['url'] = @message
  body['data'] = android_payload
  a = AndroidPush.new
  a.push(body)
end

#send_to_ios(device) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pushofy/push.rb', line 30

def send_to_ios(device)

  payload_hash = {}
  payload_hash['aps'] = {}
  payload_hash['aps']['alert'] = {}
  payload_hash['aps']['alert']['body'] = "You have a new #{@app} from #{@from}"
  payload_hash['aps']['sound'] = 'default'
  payload_hash['aps']['badge'] = 1
  payload_hash['app'] = @app
  payload_hash['url'] = @message
  device_token_hex = device.registration_id
  puts "AAAAAA"
  puts payload_hash
  ApplePush.new(payload_hash, device_token_hex).push
end