Class: APNS::Notification

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_token, message) ⇒ Notification

Returns a new instance of Notification.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/apns/notification.rb', line 10

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]
    self.message_identifier = message[:message_identifier]
    self.content_available = !message[:content_available].nil?
    self.expiration_date = message[:expiration_date]
    self.priority = if self.content_available
      message[:priority] || 5
    else
      message[:priority] || 10
    end
  elsif message.is_a?(String)
    self.alert = message
  else
    raise "Notification needs to have either a hash or string"
  end

  self.message_identifier ||= "0000"
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



6
7
8
# File 'lib/apns/notification.rb', line 6

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



6
7
8
# File 'lib/apns/notification.rb', line 6

def badge
  @badge
end

#content_availableObject

Returns the value of attribute content_available.



8
9
10
# File 'lib/apns/notification.rb', line 8

def content_available
  @content_available
end

#device_tokenObject

Returns the value of attribute device_token.



6
7
8
# File 'lib/apns/notification.rb', line 6

def device_token
  @device_token
end

#expiration_dateObject

Returns the value of attribute expiration_date.



7
8
9
# File 'lib/apns/notification.rb', line 7

def expiration_date
  @expiration_date
end

#message_identifierObject

Returns the value of attribute message_identifier.



7
8
9
# File 'lib/apns/notification.rb', line 7

def message_identifier
  @message_identifier
end

#otherObject

Returns the value of attribute other.



6
7
8
# File 'lib/apns/notification.rb', line 6

def other
  @other
end

#priorityObject

Returns the value of attribute priority.



6
7
8
# File 'lib/apns/notification.rb', line 6

def priority
  @priority
end

#soundObject

Returns the value of attribute sound.



6
7
8
# File 'lib/apns/notification.rb', line 6

def sound
  @sound
end

Instance Method Details

#packaged_messageObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/apns/notification.rb', line 59

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['aps']['content-available'] = 1 if self.content_available

  aps.merge!(self.other) if self.other
  JSON.generate(aps)
end

#packaged_notificationObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/apns/notification.rb', line 34

def packaged_notification
  pt = self.packaged_token
  pm = self.packaged_message
  pi = self.message_identifier
  pe = (self.expiration_date || 0).to_i
  pr = (self.priority || 10).to_i

  # Each item consist of
  # 1. unsigned char [1 byte] is the item (type) number according to Apple's docs
  # 2. short [big endian, 2 byte] is the size of this item
  # 3. item data, depending on the type fixed or variable length
  data = ''
  data << [1, pt.bytesize, pt].pack("CnA*")
  data << [2, pm.bytesize, pm].pack("CnA*")
  data << [3, pi.bytesize, pi].pack("CnA*")
  data << [4, 4, pe].pack("CnN")
  data << [5, 1, pr].pack("CnC")
  
  data
end

#packaged_tokenObject



55
56
57
# File 'lib/apns/notification.rb', line 55

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