Class: APNS::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/pushmeup/apns/notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_token, message) ⇒ Notification

Returns a new instance of Notification.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pushmeup/apns/notification.rb', line 5

def initialize(device_token, message)
  self.device_token = device_token
  if message.is_a?(Hash)
    self.alert = message[:alert]
    self.badge = message[:badge]
    self.sound = message[:sound]
    self.other = message[:other]
  elsif message.is_a?(String)
    self.alert = message
  else
    raise "Notification needs to have either a Hash or String"
  end
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



3
4
5
# File 'lib/pushmeup/apns/notification.rb', line 3

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



3
4
5
# File 'lib/pushmeup/apns/notification.rb', line 3

def badge
  @badge
end

#device_tokenObject

Returns the value of attribute device_token.



3
4
5
# File 'lib/pushmeup/apns/notification.rb', line 3

def device_token
  @device_token
end

#otherObject

Returns the value of attribute other.



3
4
5
# File 'lib/pushmeup/apns/notification.rb', line 3

def other
  @other
end

#soundObject

Returns the value of attribute sound.



3
4
5
# File 'lib/pushmeup/apns/notification.rb', line 3

def sound
  @sound
end

Instance Method Details

#==(that) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/pushmeup/apns/notification.rb', line 38

def ==(that)
  device_token == that.device_token &&
  alert == that.alert &&
  badge == that.badge &&
  sound == that.sound &&
  other == that.other
end

#packaged_messageObject



29
30
31
32
33
34
35
36
# File 'lib/pushmeup/apns/notification.rb', line 29

def packaged_message
  aps = {'aps'=> {} }
  aps['aps']['alert'] = self.alert if self.alert
  aps['aps']['badge'] = self.badge if self.badge
  aps['aps']['sound'] = self.sound if self.sound
  aps.merge!(self.other) if self.other
  aps.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
end

#packaged_notificationObject



19
20
21
22
23
# File 'lib/pushmeup/apns/notification.rb', line 19

def packaged_notification
  pt = self.packaged_token
  pm = self.packaged_message
  [0, 0, 32, pt, 0, pm.bytesize, pm].pack("ccca*cca*")
end

#packaged_tokenObject



25
26
27
# File 'lib/pushmeup/apns/notification.rb', line 25

def packaged_token
  [device_token.gsub(/[\s|<|>]/,'')].pack('H*')
end