Class: Pushofy::Push

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

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Push

Returns a new instance of Push.



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

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

Instance Method Details

#send_pushObject



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

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

#send_to_android(device) ⇒ Object



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

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



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

def send_to_ios(device)


  payload_hash = {
    aps: {
      alert: {
        body: "You have a new #{@app}, form #{@from}"
      },
      sound: 'default',
      badge: 1,
    },
    app: @app,
    url: @message
  }

  ApplePush.new(payload_hash, device_token_hex).push
end