Class: APNS::Notification

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/mercurius/apns/notification.rb

Constant Summary collapse

MAX_PAYLOAD_BYTES =
2048

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Notification

Returns a new instance of Notification.



10
11
12
13
# File 'lib/mercurius/apns/notification.rb', line 10

def initialize(attributes = {})
  @attributes = attributes
  super
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



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

def alert
  @alert
end

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#badgeObject

Returns the value of attribute badge.



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

def badge
  @badge
end

#otherObject

Returns the value of attribute other.



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

def other
  @other
end

#soundObject

Returns the value of attribute sound.



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

def sound
  @sound
end

Instance Method Details

#==(that) ⇒ Object



38
39
40
# File 'lib/mercurius/apns/notification.rb', line 38

def ==(that)
  attributes == that.attributes
end

#pack(device_token) ⇒ Object



24
25
26
# File 'lib/mercurius/apns/notification.rb', line 24

def pack(device_token)
  [0, 0, 32, package_device_token(device_token), 0, packaged_message.bytesize, packaged_message].pack("ccca*cca*")
end

#package_device_token(device_token) ⇒ Object



28
29
30
# File 'lib/mercurius/apns/notification.rb', line 28

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

#packaged_messageObject



32
33
34
35
36
# File 'lib/mercurius/apns/notification.rb', line 32

def packaged_message
  { aps: payload }.to_json.gsub(/\\u([\da-fA-F]{4})/) do |m|
    [$1].pack("H*").unpack("n*").pack("U*")
  end
end

#payloadObject



15
16
17
18
19
20
21
22
# File 'lib/mercurius/apns/notification.rb', line 15

def payload
  {
    alert: alert,
    badge: badge,
    sound: sound,
    other: other
  }.compact
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/mercurius/apns/notification.rb', line 42

def valid?
  packaged_message.bytesize <= MAX_PAYLOAD_BYTES
end