Class: APNS::Notification
- Inherits:
-
Object
- Object
- APNS::Notification
- Defined in:
- lib/apns/notification.rb
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#content_available ⇒ Object
Returns the value of attribute content_available.
-
#device_token ⇒ Object
Returns the value of attribute device_token.
-
#expiration_date ⇒ Object
Returns the value of attribute expiration_date.
-
#message_identifier ⇒ Object
Returns the value of attribute message_identifier.
-
#other ⇒ Object
Returns the value of attribute other.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#sound ⇒ Object
Returns the value of attribute sound.
Instance Method Summary collapse
-
#initialize(device_token, message) ⇒ Notification
constructor
A new instance of Notification.
- #packaged_message ⇒ Object
- #packaged_notification ⇒ Object
- #packaged_token ⇒ Object
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, ) self.device_token = device_token if .is_a?(Hash) self.alert = [:alert] self.badge = [:badge] self.sound = [:sound] self.other = [:other] self. = [:message_identifier] self.content_available = ![:content_available].nil? self.expiration_date = [:expiration_date] self.priority = if self.content_available [:priority] || 5 else [:priority] || 10 end elsif .is_a?(String) self.alert = else raise "Notification needs to have either a hash or string" end self. ||= "0000" end |
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
6 7 8 |
# File 'lib/apns/notification.rb', line 6 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
6 7 8 |
# File 'lib/apns/notification.rb', line 6 def badge @badge end |
#content_available ⇒ Object
Returns the value of attribute content_available.
8 9 10 |
# File 'lib/apns/notification.rb', line 8 def content_available @content_available end |
#device_token ⇒ Object
Returns the value of attribute device_token.
6 7 8 |
# File 'lib/apns/notification.rb', line 6 def device_token @device_token end |
#expiration_date ⇒ Object
Returns the value of attribute expiration_date.
7 8 9 |
# File 'lib/apns/notification.rb', line 7 def expiration_date @expiration_date end |
#message_identifier ⇒ Object
Returns the value of attribute message_identifier.
7 8 9 |
# File 'lib/apns/notification.rb', line 7 def end |
#other ⇒ Object
Returns the value of attribute other.
6 7 8 |
# File 'lib/apns/notification.rb', line 6 def other @other end |
#priority ⇒ Object
Returns the value of attribute priority.
6 7 8 |
# File 'lib/apns/notification.rb', line 6 def priority @priority end |
#sound ⇒ Object
Returns the value of attribute sound.
6 7 8 |
# File 'lib/apns/notification.rb', line 6 def sound @sound end |
Instance Method Details
#packaged_message ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/apns/notification.rb', line 59 def 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_notification ⇒ Object
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. pi = self. 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_token ⇒ Object
55 56 57 |
# File 'lib/apns/notification.rb', line 55 def packaged_token [device_token.gsub(/[\s|<|>]/,'')].pack('H*') end |