Class: ApnClient::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/apn_client/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_id, config = {}) ⇒ Message

Creates an APN message to to be sent over SSL to the APN service.

Parameters:

  • message_id (Fixnum)

    a unique (at least within a delivery) integer identifier for this message

  • device_token (String)

    A 64 byte long hex digest supplied from an app installed on a device to the server

  • alert (String)

    A text message to display to the user. Should be tweet sized (payload may not exceed 256 bytes)

  • badge (Fixnum)

    the number to show on the badge on the app icon - number of new/unread items

  • sound (String)

    filename of a sound file in the app bundle to be played to the user

  • content_available (Boolean)

    set to true if the message should trigger download of new content



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/apn_client/message.rb', line 13

def initialize(message_id, config = {})
  self.message_id = message_id
  self.device_token = device_token
  NamedArgs.assert_valid!(config,
    :optional => [:alert, :badge, :sound, :content_available],
    :required => [:device_token])
  config.keys.each do |key|
    send("#{key}=", config[key])
  end
  check_payload_size!
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



3
4
5
# File 'lib/apn_client/message.rb', line 3

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



3
4
5
# File 'lib/apn_client/message.rb', line 3

def badge
  @badge
end

#content_availableObject

Returns the value of attribute content_available.



3
4
5
# File 'lib/apn_client/message.rb', line 3

def content_available
  @content_available
end

#custom_propertiesObject

Returns the value of attribute custom_properties.



3
4
5
# File 'lib/apn_client/message.rb', line 3

def custom_properties
  @custom_properties
end

#device_tokenObject

Returns the value of attribute device_token.



3
4
5
# File 'lib/apn_client/message.rb', line 3

def device_token
  @device_token
end

#message_idObject

Returns the value of attribute message_id.



3
4
5
# File 'lib/apn_client/message.rb', line 3

def message_id
  @message_id
end

#soundObject

Returns the value of attribute sound.



3
4
5
# File 'lib/apn_client/message.rb', line 3

def sound
  @sound
end

Class Method Details

.error_codesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/apn_client/message.rb', line 34

def self.error_codes
  {
      :no_errors_encountered => 0,
      :processing_error => 1,
      :missing_device_token => 2,
      :missing_topic => 3,
      :missing_payload => 4,
      :invalid_token_size => 5,
      :invalid_topic_size => 6,
      :invalid_payload_size => 7,
      :invalid_token => 8,
      :unknown => 255
  }
end

.expires_atObject



49
50
51
52
# File 'lib/apn_client/message.rb', line 49

def self.expires_at
  seconds_per_day = 24*3600
  (Time.now + 30*seconds_per_day).to_i
end

Instance Method Details

#==(other_message) ⇒ Object



30
31
32
# File 'lib/apn_client/message.rb', line 30

def ==(other_message)
  other_message && other_message.is_a?(self.class) && other_message.message_id == self.message_id
end

#payloadObject

The payload is a JSON formated hash with alert, sound, badge, content-available, and any custom properties, example: => {“badge” => 5, “sound” => “my_sound.aiff”, “alert” => “Hello!”}



57
58
59
# File 'lib/apn_client/message.rb', line 57

def payload
  payload_hash.to_json
end

#payload_hashObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/apn_client/message.rb', line 61

def payload_hash
  result = {}
  result['aps'] = {}
  result['aps']['alert'] = alert if alert
  result['aps']['badge'] = badge if badge and badge > 0
  if sound
    result['aps']['sound'] = sound if sound.is_a? String
    result['aps']['sound'] = "1.aiff" if sound.is_a? TrueClass
  end
  result['aps']['content-available'] = 1 if content_available
  result
end

#payload_sizeObject



74
75
76
# File 'lib/apn_client/message.rb', line 74

def payload_size
  payload.bytesize
end

#to_sObject

We use the enhanced format. See the Apple documentation for details.



26
27
28
# File 'lib/apn_client/message.rb', line 26

def to_s
  [1, message_id, self.class.expires_at, 0, 32, device_token, 0, payload_size, payload].pack('ciiccH*cca*')
end